• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (deprecated)
You are here: Home / Programming / Sleep Inside JavaScript Async Function

Sleep Inside JavaScript Async Function

Last updated on March 31, 2025 by Sal Ferrarello

Typically, the point of a JavaScript async function is to not block execution but occasionally I do want to introduce a delay in an async function (e.g. as part of a delay before retrying a remote call).

This line inside an async function will introduce a delay of 10 seconds (10000 ms).

await new Promise((res) => setTimeout(res, 10000)); // Delay 10 seconds.

Example Sleep Inside Async Function

const myFunction = async () => {
  console.log('Before sleep');
  // Delay 10 seconds.
  await new Promise((res) => setTimeout(res, 10000));
  console.log('After sleep');
};
myFunction();
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP and JavaScript developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a principal engineer.

Filed Under: Programming Tagged With: JavaScript

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2026 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in