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
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
β Usage
const time = require('node-time-sleep');
(async () => {
console.log("Sleeping for 1 second...");
await time.sleep(1000); // sleep for 1000ms
console.log("Done!");
})();
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));
}
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
}
}
π Links
- π npm: https://www.npmjs.com/package/node-time-sleep
- π» GitHub: https://github.com/zegulas/node-time-sleep
π 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)