DEV Community

Cover image for await time.sleep(ms) in Node.js β€” Finally!
Ashish Kadam
Ashish Kadam

Posted on

await time.sleep(ms) in Node.js β€” Finally!

Sleep like Python in Node.js β€” Introducing node-time-sleep πŸ’€

Ever wished you could write this in Node.js?

time.sleep(1)  // Python-style pause for 1 second
Enter fullscreen mode Exit fullscreen mode

Well, now you can β€” with my tiny npm package: node-time-sleep


πŸ’‘ What is it?

node-time-sleep is a lightweight utility that lets you pause execution in Node.js without blocking the event loop, just like Python’s time.sleep() β€” but async-friendly.

πŸ“¦ Install

npm install node-time-sleep
Enter fullscreen mode Exit fullscreen mode

βœ… Usage

const time = require('node-time-sleep');

(async () => {
  console.log("Sleeping for 1 second...");
  await time.sleep(1000); // sleep for 1000ms
  console.log("Done!");
})();
Enter fullscreen mode Exit fullscreen mode

No more awkward setTimeout()s or bulky delay wrappers.


βš™οΈ How it works

Under the hood, it's just a promise-based wrapper around setTimeout:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
Enter fullscreen mode Exit fullscreen mode

But now it's packaged, documented, and ready to go. You can just drop it in and get going β€” especially handy for CLI tools, testing, mock APIs, or even fun scripting projects.


πŸš€ Why use this?

  • Familiar syntax for Python devs
  • Clean, readable async/await delays
  • Zero dependencies
  • Non-blocking (unlike busy-wait loops)
  • TypeScript support out of the box

πŸ§ͺ Example: Rate-limited API calls

const time = require('node-time-sleep');

async function crawlPages(urls) {
  for (let url of urls) {
    console.log(`Fetching ${url}...`);
    await fetch(url);
    await time.sleep(1000); // wait 1 sec before next
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”— Links


πŸ™Œ Feedback Welcome!

This is my first published package. If you have suggestions, PRs, or issues β€” I’d love to hear from you!

Drop a ⭐ on the GitHub repo if you like it!


✨ Final Thoughts

This package is intentionally simple β€” but elegant and useful for devs who want something clean, intuitive, and Pythonic.

Let me know how you use it β€” or what features you'd love to see next!

Top comments (0)