Giter Club home page Giter Club logo

async-task-group's Introduction

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)

async-task-group's People

Contributors

aleclarson avatar

Watchers

 avatar  avatar

async-task-group's Issues

[RFC] collect task results in an array?

Task groups diverge from Promise.all behavior by not collecting task results in a new array.

I'm against this being the default behavior, so we can avoid costly array allocation when not required. But how could a task group opt-in to this behavior?

Maybe you could set the results property to an array? And the promise property could resolve with the results array for convenience.

This lack of behavior is not currently documented.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.