I think you want something like this:
function setTimeoutAsync(interval, value) {
return new Promise(resolve => setTimeout(resolve, interval, value));
}
Usage:
setTimeoutAsync(1000).then(() => console.log('1 second passed'));
// or with an argument
setTimeoutAsync(1000, 1).then(n => console.log(`${n} second passed`));
Or using async/await:
async function main() {
const n = await setTimeoutAsync(1000, 1);
console.log(`${n} second passed`);
}
This solution is limited to 1 value argument, because of resolve
only accepting 1 argument, and you can't retrieve the handle from setTimeout to clear it in case you need that. Thanks to @PatrickRoberts for pointing that out.
If you need more then 1 value argument I recommend passing an array with your values or an object containing key-value pairs.