@litert/utils-async
v1.6.0
Published
The utility functions/classes/constants about asynchronous operations for JavaScript/TypeScript.
Downloads
287
Maintainers
Readme
LiteRT/Utils - Async
The utility functions/classes/constants about asynchronous operations for JavaScript/TypeScript.
Requirement
- TypeScript v5.0.0 (or newer)
- Node.js v18.0.0 (or newer)
Installation
npm i @litert/utils-async --saveUsage
API sleep
This API provides a way to pause execution for a specified duration, asynchronously.
import { sleep } from '@litert/utils-async';
await sleep(2000); // Sleep for 2 secondsAPI autoRetry
This API provides automatic retry functionality for (asynchronous) operations.
import { autoRetry } from '@litert/utils-async';
const ac = new AbortController(); // Optional, to allow aborting the retry operation
await autoRetry({
'maxRetries': 5, // Retry no more than 5 times if the first call fails
'function': async (ctx) => {
// Your function logic here
},
// Optional, `DEFAULT_BEFORE_RETRY` will be used if omitted.
'beforeRetry': async (ctx) => {
// Your beforeRetry logic here
},
// Optional, an AbortSignal to allow the retry operation to be aborted.
'signal': ac.signal,
});API withTimeout
This API binds a timeout to an asynchronous operation, without modifying the original function.
import { withTimeout } from '@litert/utils-async';
await withTimeout(5000, async () => {
// Your async code here
});
API withAbortSignal
This API binds an AbortSignal to an asynchronous operation, without modifying the original function.
import { withAbortSignal } from '@litert/utils-async';
const ac = new AbortController();
await withAbortSignal(ac.signal, async () => {
// Your async code here
});Class AbortTimeoutController
This class is an extension of the standard AbortController that automatically aborts after a specified timeout.
import { AbortTimeoutController } from '@litert/utils-async';
const atc = new AbortTimeoutController(3000); // Automatically abort after 3 seconds
// Use `atc.signal` in your async operationsClass BackgroundRunner
This class allows running asynchronous tasks in the background, managing their lifecycle.
import { BackgroundRunner } from '@litert/utils-async';
const runner = new BackgroundRunner();
runner.on('error', (err) => {
console.error('Background task error:', err);
});
// Start a background task immediately
runner.run(async () => {
// Your background task logic here
});
// Run a background task after a delay (default is in the next tick)
runner.runLater(async () => {
// Your delayed background task logic here
});Class PromiseController
Unlike the new Promise(...) flow, this class provides control over a Promise,
allowing external resolution or rejection.
import { PromiseController } from '@litert/utils-async';
const pc = new PromiseController<number>();
setTimeout(() => {
pc.resolve(42); // Resolve the promise with the value 42 after 1 second
}, 1000);
pc.promise.then((value) => {
console.log('Promise resolved with value:', value);
});Documentation
License
This library is published under Apache-2.0 license.
