Created
May 25, 2021 05:04
-
-
Save spider-man-tm/90ac3449c6b10c8a90ed3b5c43dda9c1 to your computer and use it in GitHub Desktop.
TypeScript(JavaScriptでも可)でsleep機能を実行できる関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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