synchro

execute asynchronous callbacks in sequence

npm install synchro
32 downloads in the last month

synchro

synchro lets you execute a set of asynchronous functions in sequence or to wait until all of them have finished executing.

Installation

npm install synchro

Example Code

This example creates two chains -- the first is executed in order, and the second one, out of order but waited on.

var synchro = require('synchro');

// creates a synchronous chain, same as s1 = synchro.create(async = false)
var s1 = new Synchro();

s1
  .add(function (done) {
    console.log('starting first step...');
    setTimeout(function () {
      console.log('first step DONE after 2 secs.');
      done();
    }, 2000);
  })
  .add(function (done) {
    console.log('starting second step...');
    setTimeout(function () {
      console.log('second step DONE after 3 secs.');
      done();
    }, 3000);
  })
  .add(function (done) {
    console.log('starting third step...');
    setTimeout(function () {
      console.log('third step DONE after 3 secs.');
      done();
    }, 3000);
  });

// get notified when the chain is done
s1.done(function () {
  console.log('all tasks done!');
});

// start the tasks
s1.start();

Running this code will output something like

starting first step...
first step DONE after 2 secs.
starting second step...
second step DONE after 3 secs.
starting third step...
third step DONE after 3 secs.
all tasks done!

License

(The MIT License)

Copyright (c) 2013 Jesus A. Domingo jesus.domingo@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you