

In order to "wait" in javascript using promises are the way to go as the top answers show. Using the fat arrow operator, though, makes it even smaller (and more elegant). Simple and elegant sleep function using modern Javascript function sleep(millis), millis) You can use it today by using webpack 5 (alpha), In the top level like in this example await sleep(1000) Soon you will be able to use the await syntax outside of an async function.

No need to provide command line arguments. Update June 2019: By using the latest versions of NodeJS you can use it out of the box. You can use the new async/await syntax.įor using async/await out of the box without installing and plugins, you have to use node-v7 or node-v8, using the -harmony flag. Today ( Jan 2017 June 2019) it is much easier. > await delay(1000) /// waiting 1 second.Ī new answer to an old question. > const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) Update Jan 2021: You can even do it in the Node REPL interactive using -experimental-repl-await flag $ node -experimental-repl-await
