Async.waterfall never performs a second function

Why the second function is never executed here:

async.waterfall([
    function(waterfallCb) {
        console.log("step1");
        countDocuments(db,waterfallCb);
    }, function(waterfallCb) {
        console.log("Step2");
        insertDocument(db,waterfallCb);
    }], function(err,data){
    console.log("in waterfall callback");
    db.close();
});

Conclusion:

step1
in waterfall callback

Why will the second function (which prints step 2) never be called?

EDIT: Here is countDocuments:

var countDocuments = function(db,callback){
    var collection = db.collection(colname);
    collection.find({}).toArray(function(err,docs){
        assert.equal(err, null);
        console.log("Found %d records",docs.length);
        callback(docs);
    });
};

EDIT: InsertDocument:

var insertDocument = function(db, callback){
    var collection = db.collection(colname);
    collection.insertOne(sampleDoc, function(err,result){
        assert.equal(err, null);
        assert.equal(1, result.result.n);
        assert.equal(1, result.ops.length);
        console.log("inserted 1 document into the collection");
        callback(null, 'one');
    });
};
+4
source share
1 answer

As you use caolan/async, the first argument is the error object, which in your case, if it could mean something other than zero async, that you have an error and not continue. Here is an example from the docs:

async.waterfall([
    function(callback) {
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback) {
      // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback) {
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    // result now equals 'done'    
});
+3
source

All Articles