Skip to content

Instantly share code, notes, and snippets.

@rameshbaskar
Last active October 27, 2020 07:46
Show Gist options
  • Save rameshbaskar/26a2125e0a35fbd291e634cc789e1fe4 to your computer and use it in GitHub Desktop.
Save rameshbaskar/26a2125e0a35fbd291e634cc789e1fe4 to your computer and use it in GitHub Desktop.
Waiting in NodeJS
// 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