Skip to content

Instantly share code, notes, and snippets.

@rsms
Last active March 26, 2019 09:10
Show Gist options
  • Save rsms/0dc1f20978183f1c633d to your computer and use it in GitHub Desktop.
Save rsms/0dc1f20978183f1c633d to your computer and use it in GitHub Desktop.
Simple ES7 async function example: sleep
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