You can await setTimeout
with Node.js 15 or above:
const timersPromises = require('timers/promises');
(async () => {
const result = await timersPromises.setTimeout(2000, 'resolved')
// Executed after 2 seconds
console.log(result); // "resolved"
})()
Timers Promises API: https://nodejs.org/api/timers.html#timers_timers_promises_api (library already built in Node)
Note: Stability: 1 - Use of the feature is not recommended in production environments.