Utility to wrap a function such that it executes it's inner function after n calls.

npm install bluejeansandrain.countdown
2 downloads in the last week
4 downloads in the last month


Utility to wrap a function such that it executes its inner function after n calls. Useful when calling async methods in loops and finding out when all of the async calls have returned.

countdown( count, [then], [arg, arg, ...] )

Wrap a function so that it can be called count times before calling then. If count is set to 1, then will be called the second time the wrapper function is called. If count is less than 0, it will be normalized to 0 which means then will be called the first time the wrapper is called.

then is optional and will only ever be called once.

Any extra args passed to countdown will be passed to then when it is called. The arguments passed to the wrapper call that calls then will also be passed after the args passed to countdown.

The returned wrapper has a few properties and available also.

  • count
    • The number of times the wrapper must be called before then is called.
  • index
    • Zero based index of the current call. This is null until the first call.
  • each( callback )
    • Add a function to be called every time the wrapper is called until count has been reached.
  • finish( [arg, arg, ...] )
    • This short circuits the countdown, calls then immediately, and marks the countdown as finished so that any further calls to the wrapper will have no effect.


var countdown = require( 'countdown' );

function helloworld()
    console.log( 'Hello World!', arguments );

var wrapper = countdown( 2, helloworld, 'arg1' );
console.log( 'Initial Count:', wrapper.count );

for ( var i = 2; i >= 0; --i )
    wrapper( 'arg2' );
    console.log( wrapper.index );


> Initial Count: 2
> 0
> 1
> Hello World! ["arg1", "arg2"]
> 2
npm loves you