Resolve promise after given amount of time
JavaScript, Function, Promise · Jan 8, 2022

Creates a promise that resolves after a given amount of time to the provided value.
- Use the
Promise
constructor to create a new promise. - Use
setTimeout()
to call the promise'sresolve
function with the passedvalue
after the specifieddelay
.
const resolveAfter = (value, delay) =>
new Promise(resolve => {
setTimeout(() => resolve(value, delay));
});
resolveAfter('Hello', 1000);
// Returns a promise that resolves to 'Hello' after 1s
Written by Angelos Chalaris
I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.
If you want to keep in touch, follow me on GitHub.