all-finished

3.1.3 • Public • Published

all-finished

Execute a function when pre-conditions have been satisfied (probably asynchronously).

Build Status Coverage Status Dependencies npm version Greenkeeper badge

Whilst JavaScript is single-threaded, it's async nature means that there can be multiple logical processes. all-finished is a tiny utility to assist with synchronising asynchronous calls. This is conceptually similar to Promise.all from the JavaScript promise api, or Thread.join in Java or C#.

Getting started

Install via NPM

npm i all-finished --save

Example usage

const AllFinished = require('all-finished');

const all = AllFinished();
const firstThingDone = all.track();
const secondThingDone = all.track();

all.finished((allArgs) => {
  // Do something now that firstThingDone and secondThingDone have both been executed
  console.log(allArgs[0]); // => ['first optional param']
});

setTimeout(() => {
  firstThingDone('first optional param');
}, 100);

setTimeout(() => {
  secondThingDone('second optional param');
}, 200);

API

All()

Creates a new all tracking object.

all.track()

Returns a tracking function, when all tracking functions have been executed, the finished handler is executed. Any parameters passed to the tracking function are also passed to the finished handler.

all.finished(handler)

Takes a handling function as a parameter which is executed when all created tracking functions have been executed. The handler receives the parameters passed to the tracking functions, in the order that the tracking functions were created with all.track().

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i all-finished

Weekly Downloads

1

Version

3.1.3

License

MIT

Unpacked Size

5.22 kB

Total Files

4

Last publish

Collaborators

  • toboid