javascriptnode.jscallbackunderscore.jsnimble

the callback and underscore in nimble flow-control tool


I have hard time understand nimble, and here are two questions:

1 -- I have a tiny test code below, almost direct copy from nimble's online tutorial. I didn't use any complicated asyn functions in there but just use a console.log for simplicity reasons.

var nimble = require('nimble');
nimble.series([
    function(cb) {
        console.log("first");
        cb();
    },
    function(cb) {
        console.log("second");
        cb();
    }
]);

It works as promised, but what's the point of the callback (cb)? From the code I seem never pass anything as cb to either function in the series.

Then I tried the modified version of the code. Basically, it deletes all callbacks.

var nimble = require('nimble');
nimble.series([
    function() {
        console.log("first");
    },
    function() {
        console.log("second");
    }
]);

It is sadly only output "first" but no "second", so it's wrong somewhere, not sure why. Now I am quite confused what that callback actually is and why we have to have it.

2 -- From nimble's tutorial, one seems to be able to use underscore (_) conveniently. I tried the following code, which is directly copied online, but I was just told the _ was not defined..

var nimble = require('nimble');
_.series([
    function (callback) {
        setTimeout(function () {
            console.log('one');
            callback();
        }, 25);
    },
    function (callback) {
        setTimeout(function () {
            console.log('two');
            callback();
        }, 0);
    }
]);

Probably I got something wrong. Should I import the underscore.js before I can use _ in nimble? But nimble says one of its selling points is that it is small, no need to import async and underscore. Then here I got confused again..


Solution

  • Question 1:

    nimble.series() needs the invocation of cb() in each function to tell it to move on to the next function. That's why your 2nd function didn't get executed after taking 1st function's cb() out

    Question 2:

    Yes, underscore needs to be imported before using it just like it was done with nimble

    var _ = require('underscore');