Deviopath › Node.js

Promise Delay

Easy · Async

Node's node:timers/promises module has a setTimeout(ms, value) that returns a Promise instead of taking a callback. Let's build our own.

Write a function delay(ms, value) that returns a Promise which resolves with value after ms milliseconds. Use the regular setTimeout.

The driver runDelay(ms, value) is already written. It also starts a 0 ms timer, so with a correct delay the log shows "0ms timer" first and then the resolved value.

Next: Callback to Promise