Invoking async.series inside async.series produces unpredictable output

后端 未结 1 554
盖世英雄少女心
盖世英雄少女心 2021-01-14 00:28

Using caolan\'s async library for node.js, I\'ve been trying to call a function that uses async.series inside another function that uses async.series, but I sti

相关标签:
1条回答
  • 2021-01-14 00:56

    After about an hour of experimentation, I got it to work properly. I simply modified the firstStep function so that it takes a callback function as a parameter, and calls the callback at the end of the firstStep function.

    var im = require('imagemagick');
    var async = require('async');
    
    var toObtain = false;
    
    
    var b;
    async.series([
    
    function (callback) {
        firstStep(callback); //the firstStep function takes a callback parameter and calls the callback when it finishes running. Now everything seems to be working as intended.
    },
    
    function (callback) {
        console.log("Starting the second step in the series");
        console.log("Value of b: " + b);
    }]);
    
    
    function firstStep(theCallback){
        async.series([
    
        function (next) { // step one - call the function that sets toObtain
            im.identify('kittens.png', function (err, features) {
                if (err) throw err;
                console.log("Invoking the function firstStep");
                toObtain = features;
                //console.log(toObtain);
                b = toObtain.height;
                next(); // invoke the callback provided by async
            });
        },
    
        function (next) { // step two - display it
            console.log('the value of toObtain is: %s',toObtain.toString());
            theCallback();
        }]);
    }
    
    0 讨论(0)
提交回复
热议问题