Last active
March 26, 2019 09:10
-
-
Save rsms/0dc1f20978183f1c633d to your computer and use it in GitHub Desktop.
Simple ES7 async function example: 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
function sleep(duration) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(()=> { resolve(0) }, duration); | |
}) | |
} | |
async function delayedMessage(message, delay) { | |
let remainingTime = await sleep(delay) | |
console.log(message, `(remaining time: ${remainingTime})`) | |
} | |
delayedMessage("World", 400).then(()=>{ console.log('done') }) | |
delayedMessage("Hello", 200) | |
// Output: | |
// Hello (remaining time: 0) | |
// World (remaining time: 0) | |
// done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment