All Projects → gulpjs → Bach

gulpjs / Bach

Licence: mit
Compose your async functions with elegance.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Bach

Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (+40.17%)
Mutual labels:  async, promise, parallel, callback, series
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (+13.68%)
Mutual labels:  async, promise, parallel, series
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (-43.59%)
Mutual labels:  async, promise, parallel, series
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (-71.79%)
Mutual labels:  promise, parallel, callback, series
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-82.91%)
Mutual labels:  promise, parallel, callback
Download
Download and extract files
Stars: ✭ 1,064 (+809.4%)
Mutual labels:  async, stream, promise
Write
Write data to the file system, creating any intermediate directories if they don't already exist. Used by flat-cache and many others!
Stars: ✭ 68 (-41.88%)
Mutual labels:  async, stream, promise
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (-83.76%)
Mutual labels:  stream, promise, observable
Object Observer
Object Observer functionality of JavaScript objects/arrays via native Proxy
Stars: ✭ 88 (-24.79%)
Mutual labels:  async, observable, callback
lightflow
A tiny Promise-inspired control flow library for browser and Node.js.
Stars: ✭ 29 (-75.21%)
Mutual labels:  promise, parallel, callback
P Map
Map over promises concurrently
Stars: ✭ 639 (+446.15%)
Mutual labels:  async, promise, parallel
Gollback
Go asynchronous simple function utilities, for managing execution of closures and callbacks
Stars: ✭ 55 (-52.99%)
Mutual labels:  promise, callback
Angular1 Async Filter
Angular2 async pipe implemented as Angular 1 filter to handle promises & RxJS observables
Stars: ✭ 59 (-49.57%)
Mutual labels:  promise, observable
Framework
Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.
Stars: ✭ 1,125 (+861.54%)
Mutual labels:  async, stream
Promised Pipe
A ramda.pipe-like utility that handles promises internally with zero dependencies
Stars: ✭ 64 (-45.3%)
Mutual labels:  async, promise
Emittery
Simple and modern async event emitter
Stars: ✭ 1,146 (+879.49%)
Mutual labels:  async, promise
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-58.12%)
Mutual labels:  async, promise
Promise Parallel Throttle
It's kinda like Promise.all(), but throttled!
Stars: ✭ 72 (-38.46%)
Mutual labels:  promise, parallel
Jdeferred
Java Deferred/Promise library similar to JQuery.
Stars: ✭ 1,483 (+1167.52%)
Mutual labels:  async, promise
Async
Async utilities for Golang.
Stars: ✭ 72 (-38.46%)
Mutual labels:  async, parallel

bach

NPM version Downloads Build Status AppVeyor Build Status Coveralls Status Gitter chat

Compose your async functions with elegance.

Usage

With bach, it is very easy to compose async functions to run in series or parallel.

var bach = require('bach');

function fn1(cb) {
  cb(null, 1);
}

function fn2(cb) {
  cb(null, 2);
}

function fn3(cb) {
  cb(null, 3);
}

var seriesFn = bach.series(fn1, fn2, fn3);
// fn1, fn2, and fn3 will be run in series
seriesFn(function(err, res) {
  if (err) { // in this example, err is undefined
    // handle error
  }
  // handle results
  // in this example, res is [1, 2, 3]
});

var parallelFn = bach.parallel(fn1, fn2, fn3);
// fn1, fn2, and fn3 will be run in parallel
parallelFn(function(err, res) {
  if (err) { // in this example, err is undefined
    // handle error
  }
  // handle results
  // in this example, res is [1, 2, 3]
});

Since the composer functions return a function, you can combine them.

var combinedFn = bach.series(fn1, bach.parallel(fn2, fn3));
// fn1 will be executed before fn2 and fn3 are run in parallel
combinedFn(function(err, res) {
  if (err) { // in this example, err is undefined
    // handle error
  }
  // handle results
  // in this example, res is [1, [2, 3]]
});

Functions are called with async-done, so you can return a stream, promise, observable or child process. See async-done completion and error resolution for more detail.

// streams
var fs = require('fs');

function streamFn1() {
  return fs.createReadStream('./example')
    .pipe(fs.createWriteStream('./example'));
}

function streamFn2() {
  return fs.createReadStream('./example')
    .pipe(fs.createWriteStream('./example'));
}

var parallelStreams = bach.parallel(streamFn1, streamFn2);
parallelStreams(function(err) {
  if (err) { // in this example, err is undefined
    // handle error
  }
  // all streams have emitted an 'end' or 'close' event
});
// promises
var when = require('when');

function promiseFn1() {
  return when.resolve(1);
}

function promiseFn2() {
  return when.resolve(2);
}

var parallelPromises = bach.parallel(promiseFn1, promiseFn2);
parallelPromises(function(err, res) {
  if (err) { // in this example, err is undefined
    // handle error
  }
  // handle results
  // in this example, res is [1, 2]
});

All errors are caught in a domain and passed to the final callback as the first argument.

function success(cb) {
  setTimeout(function() {
    cb(null, 1);
  }, 500);
}

function error() {
  throw new Error('Thrown Error');
}

var errorThrownFn = bach.parallel(error, success);
errorThrownFn(function(err, res) {
  if (err) {
    // handle error
    // in this example, err is an error caught by the domain
  }
  // handle results
  // in this example, res is [undefined]
});

When an error happens in a parallel composition, the callback will be called as soon as the error happens. If you want to continue on error and wait until all functions have finished before calling the callback, use settleSeries or settleParallel.

function success(cb) {
  setTimeout(function() {
    cb(null, 1);
  }, 500);
}

function error(cb) {
  cb(new Error('Async Error'));
}

var parallelSettlingFn = bach.settleParallel(success, error);
parallelSettlingFn(function(err, res) {
  // all functions have finished executing
  if (err) {
    // handle error
    // in this example, err is an error passed to the callback
  }
  // handle results
  // in this example, res is [1]
});

API

series(fns..., [extensions])

Takes a variable amount of functions (fns) to be called in series when the returned function is called. Optionally, takes an extensions object as the last argument.

Returns an invoker(cb) function to be called to start the serial execution. The invoker function takes a callback (cb) with the function(error, results) signature.

If all functions complete successfully, the callback function will be called with all results as the second argument.

If an error occurs, execution will stop and the error will be passed to the callback function as the first parameter. The error parameter will always be a single error.

parallel(fns..., [extensions])

Takes a variable amount of functions (fns) to be called in parallel when the returned function is called. Optionally, takes an extensions object as the last argument.

Returns an invoker(cb) function to be called to start the parallel execution. The invoker function takes a callback (cb) with the function(error, results) signature.

If all functions complete successfully, the callback function will be called with all results as the second argument.

If an error occurs, the callback function will be called with the error as the first parameter. Any async functions that have not completed, will still complete, but their results will not be available. The error parameter will always be a single error.

settleSeries(fns..., [extensions])

Takes a variable amount of functions (fns) to be called in series when the returned function is called. Optionally, takes an extensions object as the last argument.

Returns an invoker(cb) function to be called to start the serial execution. The invoker function takes a callback (cb) with the function(error, results) signature.

All functions will always be called and the callback will receive all settled errors and results. If any errors occur, the error parameter will be an array of errors.

settleParallel(fns..., [extensions])

Takes a variable amount of functions (fns) to be called in parallel when the returned function is called. Optionally, takes an extensions object as the last argument.

Returns an invoker(cb) function to be called to start the parallel execution. The invoker function takes a callback (cb) with the function(error, results) signature.

All functions will always be called and the callback will receive all settled errors and results. If any errors occur, the error parameter will be an array of errors.

extensions

The extensions object is used for specifying functions that give insight into the lifecycle of each function call. The possible extension points are create, before, after and error. If an extension point is not specified, it defaults to a no-op function.

extensions.create(fn, index)

Called at the very beginning of each function call with the function (fn) being executed and the index from the array/arguments. If create returns a value (storage), it is passed to the before, after and error extension points.

If a value is not returned, an empty object is used as storage for each other extension point.

This is useful for tracking information across an iteration.

extensions.before(storage)

Called immediately before each function call with the storage value returned from the create extension point.

extensions.after(result, storage)

Called immediately after each function call with the result of the function and the storage value returned from the create extension point.

extensions.error(error, storage)

Called immediately after a failed function call with the error of the function and the storage value returned from the create extension point.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].