Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chaqchase/async-sequence
async-sequence contains a utility class to run sequential promises in Typescript
https://github.com/chaqchase/async-sequence
async bun nodejs promise promise-library sequencer
Last synced: 3 days ago
JSON representation
async-sequence contains a utility class to run sequential promises in Typescript
- Host: GitHub
- URL: https://github.com/chaqchase/async-sequence
- Owner: chaqchase
- License: mit
- Created: 2023-08-13T16:17:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-23T00:38:16.000Z (about 1 year ago)
- Last Synced: 2024-12-29T00:24:37.478Z (6 days ago)
- Topics: async, bun, nodejs, promise, promise-library, sequencer
- Language: TypeScript
- Homepage:
- Size: 1.14 MB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async-sequence
**async-sequence** contains **PromiseSequencer** a utility class that enables you to execute promises sequentially with concurrency and retry capabilities. It empowers you to manage promise execution order, concurrency, and handles retries and logging seamlessly.
## Installation
Install **async-sequence** using your preferred package manager:
```bash
# Using npm
npm install @triyanox/async-sequence# Using Bun
bun add @triyanox/async-sequence# Using Yarn
yarn add @triyanox/async-sequence# Using PNPM
pnpm add @triyanox/async-sequence```
## Usage
```ts
import createPromiseSequencer from '@triyanox/async-sequence';// Example usage
const promiseSequencer = createPromiseSequencer(
// add your promises here
[() => Promise.resolve('foo'), () => Promise.reject('bar')],
{
// the maximum number of promises that can run at the same time
concurrency: 2,
// the maximum number of times a task can be retried
retryAttempts: 3,
// the delay between retries
retryDelay: 1000,
// a logger that logs the status of the PromiseSequencer
logger: {
log: (level, message) => {
console.log(`[${level.toUpperCase()}] ${message}`);
},
},
// callbacks for when a task is completed, failed or retried
onTaskCompleted: (result) => {
console.log(`Task completed with result: ${result()}`);
},
onTaskFailed: (error) => {
console.log(`Task failed with error: ${error()}`);
},
onTaskRetried: (task) => {
console.log(`Task retried: ${task}`);
},
// whether to disable logs
disableLogs: false,
},
);// use a custom generator
const promiseSequencer = createPromiseSequencer(
(function* custom() {
yield () => Promise.resolve('foo');
yield () => Promise.resolve('bar');
yield () => Promise.reject(new Error('baz'));
})(),
);// start the PromiseSequencer
promiseSequencer.start();// get the sequencer results
const results = await promiseSequencer.getResults();// stop the PromiseSequencer
promiseSequencer.stop();
```## API
### Enum: LogLevel
Represents different log levels for logging messages.
- `ERROR`: Error log level.
- `INFO`: Information log level.
- `DEBUG`: Debug log level.### Interface: Logger
A logger interface to implement custom logging behavior.
- `log(level: LogLevel, message: string): void`: Logs a message at the specified log level.
### Interface: PromiseSequencerOptions\
Options for configuring the PromiseSequencer.
- `promiseGenerator`: A generator that yields promises.
- `concurrency` (optional): The number of promises to run concurrently. Default: 1.
- `retryAttempts` (optional): The number of times to retry a failed task. Default: 0.
- `retryDelay` (optional): The delay in milliseconds between retries. Default: 0.
- `logger` (optional): A logger instance for custom logging. Default: console.
- `disableLogs` (optional): Whether to disable logging. Default: false.
- `onTaskCompleted` (optional): A callback for task completion.
- `onTaskFailed` (optional): A callback for task failure.
- `onTaskRetried` (optional): A callback for task retries.
- `throwOnErrors` (optional): Whether to throw an error when a task fails. Default: false.### Class: PromiseSequencer\
A class for executing promises sequentially with concurrency and retry capabilities.
- `constructor(options: PromiseSequencerOptions)`: Creates a new PromiseSequencer instance.
- `start(): Promise`: Starts executing promises.
- `stop()`: Stops the PromiseSequencer.
- `getQueue(): (() => Promise)[]`: Returns the current queue of tasks.
- `getRunningTasks(): (() => Promise)[]`: Returns the current running tasks.
- `getCompletedTasks(): (() => Promise)[]`: Returns the completed tasks.
- `getFailedTasks(): (() => Promise)[]`: Returns the failed tasks.
- `getRetryTasks(): (() => Promise)[]`: Returns the tasks that are currently being retried.
- `getResults(): Promise`: Returns the results of the PromiseSequencer if the `throwOnErrors` option is set to `false` it will return null for the failed tasks if not it will throw an error.
- `setCurrentConcurrency(concurrency: number): void`: Sets the current concurrency.### Function: createPromiseSequencer\
Creates a new PromiseSequencer instance.
- `promiseGenerator`: A generator that yields promises.
- `options` (optional): Options for configuring the PromiseSequencer.## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.