Skip to content

Instantly share code, notes, and snippets.

@nichoth
Last active March 28, 2024 19:58
Show Gist options
  • Save nichoth/9764e87f843f8a0cd7f5682ae6a3e526 to your computer and use it in GitHub Desktop.
Save nichoth/9764e87f843f8a0cd7f5682ae6a3e526 to your computer and use it in GitHub Desktop.
Async sleep — return a promise that waits for ms
/**
* Sleeps for `ms` milliseconds.
* @param {number} ms
* @return {Promise}
*/
async function sleep (ms:number) {
await new Promise((resolve) => {
if (!ms) {
process.nextTick(resolve)
} else {
setTimeout(resolve, ms)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment