Skip to content

Count callbacks before continuing, tiny control flow helper, allows dynamic counting. Works great with promises.

Notifications You must be signed in to change notification settings

tjmehta/callback-count

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

callback-count Build Status

Count callbacks before continuing, tiny control flow helper, allows dynamic counting.

Flow control

var callbackCount = require('callback-count');
var counter = callbackCount(done);

setTimeout(counter.inc().next, 100);
setTimeout(counter.inc().next, 100);

function done (err) {
  console.log('finished.');
}

.inc() allows you to dynamically update the number of callbacks you are expecting.

var callbackCount = require('callback-count');
var counter = callbackCount(done);

counter.inc().inc().inc();
counter.next().next().next();

function done (err) {
  console.log('finished.');
}

The constructor can take an initial value for the count expected

var callbackCount = require('callback-count');
var counter = callbackCount(3, done);

counter.next().next().next();

function done (err) {
  console.log('finished.');
}

.next() decrements the count and callsback when the count has reached 0

var counter = createCounter(3, done);

counter.next().next().next();

function done (err) {
  console.log(counter.count); // 0
  console.log('finished.');
}

if .next() receives an error it will callback immediately

var counter = createCounter(3, done);

counter.next(new Error('boom'));

function done (err) {
  console.log(err.message); // boom
}

License: MIT

About

Count callbacks before continuing, tiny control flow helper, allows dynamic counting. Works great with promises.

Resources

Stars

Watchers

Forks

Packages