Skip to content

Instantly share code, notes, and snippets.

@kdabir
Last active May 14, 2017 14:26
Show Gist options
  • Save kdabir/4138b82f3203e7f6c2a3 to your computer and use it in GitHub Desktop.
Save kdabir/4138b82f3203e7f6c2a3 to your computer and use it in GitHub Desktop.
Sleep with a promise
sleep = (ms) ->
new Promise (resolve, reject) ->
try
setTimeout(resolve, ms)
catch e
reject(e)
return
sleep(1000).then( -> console.log "hola" )
const sleep = (ms) =>
new Promise( (resolve, reject) => {
try { setTimeout(resolve, ms) } catch (e) { reject(e) }
})
async function use() {
console.log("before sleep");
await sleep(2000);
console.log("after sleep");
}
use();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment