sleep

JavaScript, Function, Promise

Delays the execution of an asynchronous function.

  • Delay executing part of an async function, by putting it to sleep, returning a new Promise().
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function sleepyWork() {
  console.log("I'm going to sleep for 1 second.");
  await sleep(1000);
  console.log('I woke up after 1 second.');
}

Recommended snippets

  • pipeAsyncFunctions

    JavaScript, Function

    Performs left-to-right function composition for asynchronous functions.

  • debouncePromise

    JavaScript, Function

    Creates a debounced function that returns a promise, but delays invoking the provided function until at least ms milliseconds have elapsed since the last time it was invoked. All promises returned during this time will return the same data.

  • promisify

    JavaScript, Function

    Converts an asynchronous function to return a promise.