node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป



Simple helper for executing functions only after others have completed. Inspired by this StackOverflow thread.


In terminal:

npm install when-then


In your JavaScript:

var when = require('when-then');

when([func1, func2, func3]).then(callback);

when takes a list of functions, which it executes internally with a callback as its only parameter. This callback takes no arguments, and is used by when internally to determine when functions have completed. then executes once all of those functions have completed. A more realistic use-case might look like this:

var when = require('when-then')
  , twitter_handles = [...]
  , data_about_handles = []
  , function_list = [];

var get_handle_data = function(handle) {
	return function(cb) {

for(var i = 0; i < twitter_handles.length; i++) {

when(function_list).then(function() {
	// do something with data_about_handles

In the above example, we generate functions for everything we want to accomplish, push it to a list of functions, and then use when to execute them all. We store the data returned by our function list in a variable that is then available to our then callback.


To run tests, install the library, and then run:

npm test when-then