Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
await interval function
const interval = async (ms, callback, continueCond) => {
const _interval = async () => {
await new Promise(resolve => {
setTimeout(resolve, ms);
});
if (continueCond()) {
callback();
await _interval();
}
};
await _interval();
}
// example
const main = async () => {
const intervalMs = 3000;
console.log("start!!");
// 3秒毎にカウント
let count = 0;
const callback = (counter) => {
console.log("[Info] count: ", counter);
count++;
};
const continueCond = (counter) => 10 > counter;
await interval(
3000,
() => callback(count),
() => continueCond(count),
);
console.log("finish!!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.