Javascript sleep or wait using promises
The following utility function allows a “sleep” or wait period while processing a promise tree:
const wait = async ms => {
ms = ms || 1000
console.log(`waiting ${ms} msecs ... `)
return new Promise(resolve => setTimeout(() => resolve(), ms))
}
e.g.
; (async () => {
await wait(~~(Math.random()*10000))
.then(() => console.log('done waiting'))
})()