Last active
October 27, 2020 07:46
-
-
Save rameshbaskar/26a2125e0a35fbd291e634cc789e1fe4 to your computer and use it in GitHub Desktop.
Waiting in NodeJS
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
// Using util.promisify() | |
async function sleep(ms) { | |
console.log('Waiting...'); | |
await require('util').promisify(setTimeout)(ms); | |
} | |
console.log('initial log'); | |
sleep(5000).then(() => { | |
console.log('after sleep'); | |
}); | |
// Using native Promise | |
async function pause(ms) { | |
console.log('Waiting...'); | |
await new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(null); | |
}, ms); | |
}); | |
} | |
console.log('initial log'); | |
pause(5000).then(() => { | |
console.log('after sleep'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment