LoginSignup
6
3

More than 5 years have passed since last update.

node.js で sleep

Last updated at Posted at 2018-11-25

なんか Node でコマンドラインツールを書いていて sleep したい用事があったので作ってみたらこうなった。

async function sleep(t) {
  return await new Promise(r => {
    setTimeout(() => {
      r();
    }, t);
  });
}
async function main() {
  while (true) {
    console.log("sleep...");
    await sleep(1000);
  }
}
main();

https://github.com/tc39/proposal-top-level-await これが取り込まれると main がいらなくなってハッピー。

※ 書いたあとで気がついたけど

https://qiita.com/albno273/items/c2d48fdcbf3a9a3434db で同じ方式が説明されていた。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3