Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

⏳ A utility to delay a promise for a specified amount of time.

License

Notifications You must be signed in to change notification settings

shahradelahi/ts-delay

Repository files navigation

@se-oss/delay
CI NPM Version MIT License npm bundle size Install Size

@se-oss/delay is a lightweight utility to delay a promise for a specified amount of time, offering a modern alternative to setTimeout.


📦 Installation

npm install @se-oss/delay
Install using your favorite package manager

pnpm

pnpm install @se-oss/delay

yarn

yarn add @se-oss/delay

📖 Usage

Basic Delay

import delay from '@se-oss/delay';

await delay(1000);

Delay with a value

The returned promise resolves with a specified value.

import delay from '@se-oss/delay';

const result = await delay(100, { value: '☕' });
console.log(result);
//=> '☕'

Execution Stats

Retrieve the actual execution drift (difference between requested and actual delay).

import delay from '@se-oss/delay';

const { value, stats } = await delay(100, { value: '☕', stats: true });

console.log(stats.drift);
//=> 2 (milliseconds)

Random Delay

Delay for a random amount of time within a specified range.

import { rangeDelay } from '@se-oss/delay';

await rangeDelay(100, 200);

Clear a Delay

Clear a pending delay to resolve it immediately.

import delay, { clearDelay } from '@se-oss/delay';

const promise = delay(1000, { value: '☕' });

// Sometime later...
clearDelay(promise);

const result = await promise;
//=> '☕'

Abort Signal

Abort a delay using an AbortSignal.

import delay from '@se-oss/delay';

const controller = new AbortController();

setTimeout(() => controller.abort(), 500);

try {
  await delay(1000, { signal: controller.signal });
} catch (error) {
  console.log(error.name);
  //=> 'AbortError'
}

📚 Documentation

For all configuration options, please see the API docs.

🤝 Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.

Thanks again for your support, it is much appreciated! 🙏

License

MIT © Shahrad Elahi and contributors.

About

⏳ A utility to delay a promise for a specified amount of time.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •