Emanegux
Emanegux

Reputation: 1120

NPM Async Series Issue

I am having an issue using the npm async module. The documentation suggests the following code structure will result in sequential execution of the functions added to the series array but when I run the following, I only get the "first" console.log. What is the issue?

var async  = require('async');

async.series([
  function(){ console.log("first")},
  function(){ console.log("second")}
]);

Upvotes: 0

Views: 139

Answers (3)

Nir Levy
Nir Levy

Reputation: 12953

when using async.series, you should provide each function with a callback function, which when executed it 'tells' async to move forward to the next function in line:

async.series([
  function(callback){ console.log("first"); callback();},
  function(callback){ console.log("second"); callback();}
]);

Upvotes: 1

adeneo
adeneo

Reputation: 318212

Async expects a callback to be called when each function is done, to then fire the next function etc.

var async  = require('async');

async.series([
  function(callback){ 
      console.log("first");
      callback(null, 'data'); // first arg is error
  },
  function(callback){ 
      console.log("second")
      callback(null, 'data'); // first arg is error
  }
], function(err, results) {
    // results === ['data', 'data']
});

The documentation is somewhat lacking in explaining this

Upvotes: 1

TAGraves
TAGraves

Reputation: 1409

async.series passes a callback that you must call at the end of each function:

var async  = require('async');

async.series([
  function(callback){ console.log("first"); callback(null, 1); },
  function(callback){ console.log("second"); callback(null, 2);}
]);

Upvotes: 1

Related Questions