2 ms·
Also, wrapping callback APIs or containing side effects: function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } await sleep
by 9dev 3y ago
Also, wrapping callback APIs or containing side effects:
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
await sleep(100);
There are some use cases for the Promise class still, and it’s great to have that kind of control facility at hand when you need it.
- M2Ys4U 3y agoPromise.withResolvers can fill this gap now (though it's not natively available in Node, yet): function sleep(ms) { const { promise, resolve } = Promise.withResolvers(); return setTimeout(resolve, ms), promise; }