Skip to content

Instantly share code, notes, and snippets.

@spider-man-tm
Created May 25, 2021 05:04
Show Gist options
  • Save spider-man-tm/90ac3449c6b10c8a90ed3b5c43dda9c1 to your computer and use it in GitHub Desktop.
Save spider-man-tm/90ac3449c6b10c8a90ed3b5c43dda9c1 to your computer and use it in GitHub Desktop.
TypeScript(JavaScriptでも可)でsleep機能を実行できる関数
// Promise, async/awaitを使えば簡単にかける
/**
* msec: スリープさせる時間(秒)
* 戻り値はPromise
*/
const sleep = (msec) => new Promise(resolve => setTimeout(resolve, msec * 1000));
/**
* async/awaitでスリープ機能を実装
*/
const main = async () => {
console.log('start');
await sleep(3);
console.log('3秒経過');
await sleep(4);
console.log('7秒経過');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment