@mrhotmadm/sleep
v1.0.0
Published
Uses promises and awaits to pause execution, similar to Python's time.sleep() and Lua's wait().
Downloads
2
Readme
This package uses promises and awaits to pause execution, similar to Python's time.sleep()
and Lua's wait()
.
Installation
You can download this package by using the command; npm i @mrhotmadm/sleep
Using the package
The most simplest way to use this package is to...
- Require the package.
const sleep = require('@mrhotmadm/sleep');
- Call the main function (inside of an async function).
const sleep = require('@mrhotmadm/sleep');
async function main() {
sleep();
}
- Finally, pass in the amount of milliseconds to wait.
const sleep = require('@mrhotmadm/sleep');
async function main() {
await sleep(5000); // Waits for 5 seconds.
// Your code after the function is done.
}
Options
If you want to use normal numbers (e.g. wait 5 seconds) without converting to milliseconds, you can pass in a Boolean for the second argument. Lets say you don't specify a second argument, thats fine. It will assume you are using milliseconds.
const sleep = require('@mrhotmadm/sleep');
async function main() {
await sleep(5, false); // Waits for 5 seconds, tells the function that the first argument is not in milliseconds, so it converts for you.
// Your code after the function is done.
}
Ways to use asynchronously
The most simplest and ideal way to use this package is to just add await
before calling the package. This will automatically make it wait for the function to return the promise.
You can also add a .catch
to handle errors properly, .then
is unneccessary if you are in a big project.
const sleep = require('@mrhotmadm/sleep');
async function main() {
await sleep(5000) // Waits for 5 seconds.
.catch((error) => console.error(error)); // Optional!
// Your code after the function is done.
}
The other way you can use this is adding .then
after calling the function, you won't really need the await
at the beginning, but I think it takes up too much space and time.
Usage
It's logical to make something like this open-sourced as most experienced people can code this without any reference and because of it's simplicity. This part might be unneccessary but feel free to copy the code from the git repository.
Thanks for downloading!