NodeJS async.Whilst

var count = 0;

async.whilst(
    function () { return count < 5; },
    function (callback) {
        count++;
        setTimeout(callback, 1000);
    },
    function (err) {
        // 5 seconds have passed
    }
);

is there any way that the first function passes the variable to the second function being processed. For instance:

async.whilst(
    // if EOF data will evaluate to false
    // otherwise, data will be an object
    function () { var data = processSomeDataSync(); return data },
    function (data, callback) {
        process(data)
    },
    function (err) {
    }
);
+3
source share
1 answer

Add a new scope for the entire call async.whilstand make a datalocal variable for that scope:

(function() {
    var data = null;

    async.whilst(
        // if EOF data will evaluate to false
        // otherwise, data will be an object
        function () { 
            data = processSomeDataSync(); return data != null; 
        },
        function (callback) {
            process(data)
        },
        function (err) {
        }
    );
})();
+5
source

All Articles