Skip to content
Nate
Stephens

Delay Function - Promise Based

Sometimes you need to create an artificial time delay in your code...usually for testing a UI's handling of a delayed API response.

This function will pause execution of code in an async function.

const wait = (seconds) =>
  new Promise((resolve) => setTimeout(resolve, seconds * 1000));

In use:

const foo = async () => {
  // ...function code
  await wait(3); // wait for 3 seconds
  // ...rest of function code
};

Last Updated: