async-task-group
TypeScript icon, indicating that this package has built-in type declarations

1.3.3 • Public • Published

async-task-group

The AsyncTaskGroup class is like Promise.all, but more flexible.

Tasks can be added any time before completion, which means tasks can easily extend the lifetime of an AsyncTaskGroup by adding more tasks.

The number of concurrent tasks can be limited or unlimited. When limited, any remaining tasks are not processed if a preceding task throws an error.

"Tasks" can be any value type. "Task functions" are called automatically. "Task promises" are awaited. If you pass a "wrap function" to the constructor, all task values will be passed to it. Typically, your wrap function should return a promise, but any value type is acceptable. Any functions returned by your wrap function are not called automatically.

const AsyncTaskGroup = require('async-task-group')

// Limit task concurrency by passing a number (optional).
const tasks = new AsyncTaskGroup(5)

tasks.push(() => {
  // Tasks can be sync or async.
})

// Add an array of tasks.
tasks.concat([ task1, task2 ])

// Map values of an array into tasks.
tasks.map(array, (value, index) => {
  // This return value must be a task.
  return () => value
})

// Wait for all tasks to finish.
const promise = tasks.then(() => {})

// Attach an error handler.
const promise = tasks.catch(e => {})

// Provide a "wrap function" that maps every task value.
const tasks = new AsyncTaskGroup(2, fetch)

Readme

Keywords

none

Package Sidebar

Install

npm i async-task-group

Weekly Downloads

12

Version

1.3.3

License

MIT

Unpacked Size

4.8 kB

Total Files

5

Last publish

Collaborators

  • aleclarson