@loxjs/sleep
v2.0.5
Published
A simple promise-based sleep function for Node.js, `@loxjs/sleep` provides a straightforward way to introduce a delay in your async functions or promise chains.
Downloads
9
Readme
@loxjs/sleep
A simple promise-based sleep function for Node.js, @loxjs/sleep
provides a straightforward way to introduce a delay in your async functions or promise chains.
Installation
Install the module with npm:
npm install @loxjs/sleep
Or with yarn:
yarn add @loxjs/sleep
Usage
To use @loxjs/sleep
, simply require it in your module and call it with an amount of time in milliseconds.
const sleep = require('@loxjs/sleep')
// In an async function
async function example() {
console.log('Sleeping for 2 seconds...')
await sleep(2000); // Sleeps for 2000 milliseconds (2 seconds)
console.log('Woke up after 2 seconds!')
}
// Immediately-Invoked Function Expression (IIFE) with async
(async () => {
console.log('Sleeping for 3 seconds...')
await sleep(3000); // Sleeps for 3000 milliseconds (3 seconds)
console.log('Woke up after 3 seconds!')
})()
API
@loxjs/sleep
exports a single function:
sleep(time)
Returns a Promise that resolves after the specified time.
Parameters
time
(Number) - The amount of time to sleep in milliseconds.
Returns
Promise
- A Promise that resolves after the specified amount of time.
Example
const sleep = require('@loxjs/sleep')
async function fetchData() {
// Simulate a delay in fetching data
console.log('Fetching data in 1 second...')
await sleep(1000)
console.log('Data fetched!')
}
fetchData()
Contributing
Contributions to @loxjs/sleep
are welcome! Please ensure that your contributions adhere to the following guidelines:
- Write clear, readable, and maintainable code.
- Follow existing coding styles and practices.
- Write meaningful commit messages.
- Update the documentation accordingly.
For more detailed information, please read the contributing guide.
Enjoy using @loxjs/sleep
!