Resolve promise after given amount of time

Angelos Chalaris · 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's resolve function with the passed value after the specified delay.
const resolveAfter = (value, delay) =>
  new Promise(resolve => {
    setTimeout(() => resolve(value, delay));
resolveAfter('Hello', 1000);
// Returns a promise that resolves to 'Hello' after 1s

Would you like to help us improve 30 seconds of code?Take a quick survey

Recommended snippets

  • Debounce promise

    JavaScript, Function

    Creates a debounced function that returns a promise.

  • Repeat generator

    JavaScript, Function

    Creates a generator, repeating the given value indefinitely.

  • Range generator

    JavaScript, Function

    Creates a generator, that generates all values in the given range using the given step.