@se-oss/delay
v1.1.0
Published
A lightweight utility to delay a promise for a specified amount of time.
Maintainers
Readme
@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/delaypnpm
pnpm install @se-oss/delayyarn
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.
