NodeJS equivalent to Python's time.sleep() using async/await
Install the sleep-promise package:
nodejs_sleep_async_example.sh
npm install --save sleep-promise
then you can use
example.js
const sleep = require('sleep-promise');
// In any async function:
await sleep(2000); // Wait 2000 ms
Full example:
example.js
const sleep = require('sleep-promise');
(async () => {
console.log("This prints immediately");
await sleep(2000);
console.log("This prints 2 seconds later");
})();
Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow