patternjavascriptCritical
What is the JavaScript version of sleep()?
Viewed 0 times
thesleepwhatjavascriptversion
Problem
Is there a better way to engineer a
Unlike Sleep in JavaScript - delay between actions, I want a real sleep in the middle of a function, and not a delay before a piece of code executes.
sleep in JavaScript than the following pausecomp function?function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}Unlike Sleep in JavaScript - delay between actions, I want a real sleep in the middle of a function, and not a delay before a piece of code executes.
Solution
2017 — 2021 update
Since 2009 when this question was asked, JavaScript has evolved significantly. All other answers are now obsolete or overly complicated. Here is the current best practice:
Or as a one-liner:
As a function:
or in Typescript:
use it as:
Demo:
Two new JavaScript features (as of 2017) helped write this "sleep" function:
Compatibility
If for some reason you're using Node older than 7 (which reached end of life in 2017), or are targeting old browsers,
Since 2009 when this question was asked, JavaScript has evolved significantly. All other answers are now obsolete or overly complicated. Here is the current best practice:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}Or as a one-liner:
await new Promise(r => setTimeout(r, 2000));As a function:
const sleep = ms => new Promise(r => setTimeout(r, ms));or in Typescript:
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));use it as:
await sleep();Demo:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function demo() {
for (let i = 0; i
Note that,
await can only be executed in functions prefixed with the async keyword, or at the top level of your script in an increasing number of environments.
await only pauses the current async function. This means it does not block the execution of the rest of the script, which is what you want in the vast majority of the cases. If you do want a blocking construct, see this answer using Atomics`.wait, but note that most browsers will not allow it on the browser's main thread.Two new JavaScript features (as of 2017) helped write this "sleep" function:
- Promises, a native feature of ES2015 (aka ES6). We also use arrow functions in the definition of the sleep function.
- The
async/awaitfeature lets the code explicitly wait for a promise to settle (resolve or reject).
Compatibility
- promises are supported in Node v0.12+ and widely supported in browsers, except IE
async/awaitlanded in V8 and has been enabled by default since Chrome 55 (released in Dec 2016)
- it landed in Node 7 in October 2016
- and also landed in Firefox Nightly in November 2016
If for some reason you're using Node older than 7 (which reached end of life in 2017), or are targeting old browsers,
async/await can still be used via Babel (a tool that will transpile JavaScript + new features into plain old JavaScript), with the transform-async-to-generator plugin.Code Snippets
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}await new Promise(r => setTimeout(r, 2000));const sleep = ms => new Promise(r => setTimeout(r, ms));const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));await sleep(<duration>);Context
Stack Overflow Q#951021, score: 5690
Revisions (0)
No revisions yet.