2.0.1 • Published 3 years ago

all-aboard v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

A small Javascript library to perform parallel and/or sequential asynchronous tasks with ease.

Installation

yarn add all-aboard
npm install all-aboard

Upgrade

yarn upgrade all-aboard --latest

For more information, please refer to the yarn documentation.

npm update all-aboard

For more information, please refer to the npm documentation.

Usage

Sequence

The sequence function allows you to run asynchronous functions in order, one after the other, before proceeding on the main thread. Each asynchronous function is executed in incremental order, meaning that tasks[0] runs first, tasks[1] after that, and so on.

Uniquely to the sequence function, you have the option to be ejected back on the main thread if any of the asynchronous tasks are rejected. This means that if a task throws an error in the middle, the sequence function will not continue to run the remaining tasks.

Structure

const response = await sequence(tasks[], arguments[], eject);

Example

const { sequence } = require('all-aboard');

const arr = [async1, async2, async3];

(async () => {
  await sequence(arr, ['shared', 'arguments'], true);
})();

Parallel

The parallel function allows you to run asynchronous functions in parallel, non-blocking behavior, before proceeding on the main thread. Each asynchronous function will be executed as soon as possible, but the main thread will not proceed until each promise has been settled.

Structure

const response = await parallel(tasks[], arguments[]);

Example

const { parallel } = require('all-aboard');

(async () => {
  await parallel(() => asyncFunc('with argument'), asyncFunc);
})();

Arguments

If you want to run an asynchronous function without passing any arguments, you can pass the function as a task to the sequence or parallel function.

await parallel(asyncFunc);

Specific

Things become more tricky if you want to run an asynchronous function with some passed arguments. In that case, you need to either:

  1. Wrap the asynchronous function in an anonymous function.
await sequence(() => asyncFunc('foo'));
  1. Bind the desired arguments to the passed asynchronous function.
await parallel(asyncFunc.bind(this, 'bar', 'baz'));

Shared

You might come across a use case where you would like to pass the same arguments to each asynchronous function in your tasks. Instead of repeating yourself, you can pass all shared arguments to the sequence or parallel function by passing them in an array after the tasks argument.

await sequence(asyncFuncArr, ['foo', 'bar']);

Please note that shared arguments can be overwritten by specific arguments

Response

Each response contains a promise summary of each executed task. If the status is fulfilled, the object will contain a value property containing the returned value from the successful task. If the status has been rejected, the object will instead contain a reason property with thrown error from the unsuccessful task.

[
  { status: 'fulfilled', value: undefined },
  { status: 'fulfilled', value: 'returned value' },
  { status: 'rejected', reason: 'thrown error' }
];

Uninstall

yarn remove all-aboard
npm uninstall all-aboard