

If you are used to writing in multi-threaded languages, you probably have the idea of yielding execution for a set amount of time until your thread wakes up. I think it's perfectly reasonable to want to perform an action, wait, and then perform another action. 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.

Promises, a native feature of ES2015 (aka ES6).Two new JavaScript features (as of 2017) helped write this "sleep" function: wait, but note that most browsers will not allow it on the browser's main thread. If you do want a blocking construct, see this answer using Atomics. 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.

