7

I'm not able to get my asynchronous code working with node.js

Trying both async and step libraries -- the code only returns the first function (doesn't seem to go through the rest). What am I doing wrong?

thanks!

var step = require('step');
step(
function f1(){
        console.log('test1');
},
function f2(){
        console.log('test2');
},
function finalize(err) {
    if (err) { console.log(err);return;}
    console.log('done with no problem');
  }
);

or THIS:

var async = require('async');
async.series([
function f1(){
        console.log('test1');
},
function f2(){
        console.log('test2');
},
function finalize(err) {
    if (err) { console.log(err);return;}
    console.log('done with no problem');
  }
]);
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
NikG
  • 195
  • 1
  • 2
  • 10

2 Answers2

3

Step.js is expecting callbacks for each step. Also the function that is using Step should callback with the result and not return it.

So let's say you have:

function pullData(id, callback){
  dataSource.retrieve(id, function(err, data){
    if(err) callback(err);
    else callback(data);
  });
}

Using Step would work like:

var step = require('step');

function getDataFromTwoSources(callback){
  var data1,
    data2;
  step(
    function pullData1(){
      console.log('test1');
      pullData(1, this);
    },
    function pullData2(err, data){
      if(err) throw err;
      data1 = data;
      console.log('test2');
      pullData(2, this);
    },
    function finalize(err, data) {
      if(err)
        callback(err);
      else {
        data2 = data;
        var finalList = [data1, data2];
        console.log('done with no problem');
        callback(null, finalList);
      }
    }
  );
};

This would get it to proceed through the steps.

Note that I personally prefer async for two reasons:

  • Step catches all thrown errors and does a callback with them; this changes the behavior of the application, when these libraries should just be cleaning up the look of the code.
  • Async has much better combination options and looks cleaner
Oved D
  • 7,132
  • 10
  • 47
  • 69
  • is doSomething() the callback function in each of the functions, i.e. f1 and f2? – NikG Jul 18 '12 at 03:25
  • I guess I'm not understanding callbacks very well; trying to get at 2 separate functions that retrieve results from a db into an array, and then the second function that processes the array. i figured having them sequential is the right way to do it (or nested), but not sure what the callbacks structure should be. sorry not more clear on this :( – NikG Jul 18 '12 at 03:34
  • what does "callback(null, {a : '5'});" do specifically? – NikG Jul 18 '12 at 04:10
  • with node.js, every callback should have two arguments: the first is the error, and the second is the data that is passed back. So "callback(null, {a : '5'});" is essentially saying there was no error, and you are returning data - {a : '5'}. I will flush out the examples more. – Oved D Jul 18 '12 at 15:44
2

I can only speak to async, but for that your anonymous functions in the array you pass to async.series must call the callback parameter that is passed into those functions when the function is done with its processing. As in:

async.series([
    function(callback){
        console.log('test1');
        callback();
    },
    function(callback){
        console.log('test2');
        callback();
    }
]);
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • I tested this, and of course it worked. I guess the callback is required. what's the point of the callback though when there's nothing really to do except execute the code serially? – NikG Jul 18 '12 at 03:26
  • 1
    Because typically your `async.series` function has an asynchronous aspect to it, and you'll only call the callback when that's completed (potentially long after the function itself has returned). – JohnnyHK Jul 18 '12 at 03:30
  • 1
    It gets passed into the top-level callback that you provide as the second parameter to `async.series`. See the [docs](https://github.com/caolan/async#series). – JohnnyHK Jul 18 '12 at 03:38