1.0.3 • Published 1 year ago

@noxic/sleep v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago
# @noxic/sleep

A simple `sleep` function to pause the execution for a given number of milliseconds.

## Installation

You can install the package using npm:
npm install @noxic/sleep

Usage

CommonJS

const { sleep } = require('@noxic/sleep');

// Using .then()
sleep(1000).then(() => {
  console.log('Waited for 1 second');
});

// Using async/await
async function example() {
  console.log('Waiting for 1 second...');
  await sleep(1000);
  console.log('1 second has passed');
}

example();

// can also be used outside functions like this:

console.log('Waiting for 1 second...');
await sleep(1000)
console.log('1 second has passed');

// Allows math

console.log('Waiting for 1 hour...');
await sleep(1 * 60 * 1000)
console.log('1 hour has passed');

ESM

import { sleep } from '@noxic/sleep';

// Using .then()
sleep(1000).then(() => {
  console.log('Waited for 1 second');
});

// Using async/await
async function example() {
  console.log('Waiting for 1 second...');
  await sleep(1000);
  console.log('1 second has passed');
}

example();

// can also be used outside functions like this:

console.log('Waiting for 1 second...');
await sleep(1000)
console.log('1 second has passed');

// Allows math

console.log('Waiting for 1 hour...');
await sleep(1 * 60 * 1000)
console.log('1 hour has passed');

Examples

Using .then()

The sleep function can be used with the .then() method to execute code after a delay.

const sleep = require('@noxic/sleep');

sleep(2000).then(() => {
  console.log('Waited for 2 seconds');
});

Using async/await

The sleep function can also be used with async/await for a more synchronous-like code flow.

import sleep from '@noxic/sleep';

async function run() {
  console.log('Starting...');
  await sleep(3000);
  console.log('Waited for 3 seconds');
}

run();

License

This project is licensed under the ISC License.

In this `README.md` file:
- **Installation** section shows how to install the package.
- **Usage** sections demonstrate how to use the `sleep` function with both CommonJS and ESM formats.
- **Examples** section includes examples of using the `sleep` function with `.then()` and `async/await`.
- **License** section provides information about the license for the package. Adjust the license type if it is different from ISC.
1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago