NodeJS parallel不等待并行函数

5cnsuln7  于 2023-10-17  发布在  Node.js
关注(0)|答案(3)|浏览(129)

运行下面的代码,我只是得到“undefined”。parallel调用似乎在4个并行函数完成之前执行了最后一个函数。

function classify(filename) {
    var stack = [];

    var functionOne = async function(callback){
        //call to an API
        res = await classifierOne(filename);
        callback(null, res);
    }
    var functionTwo = async function(callback){
        //call to an API
        res = await classifierTwo(filename);
        callback(null, res);
    }
    var functionThree = async function(callback){
        //call to an API
        res = await classifierThree(filename);
        callback(null, res);
    }
    var functionFour = async function(callback){
        //call to an API
        res = await classifierFour(filename);
        callback(null, res);
    }

    stack.push(functionOne);
    stack.push(functionTwo);
    stack.push(functionThree);
    stack.push(functionFour);

    async.parallel(stack, function(err, res){
        console.log(res[0]);
    })
}
biswetbf

biswetbf1#

如果你使用3.x版本的async,我们有一个很大的变化-支持async/await,版本,已经 Package 到Promise的任务(当你使用callback(null, value)关键字时),那么callback将被禁用,你需要在任务中返回值而不是调用callback(null, value)
这意味着,要修复您的案例,代码将为:

function classify(filename) {
    var stack = [];

    var functionOne = async function(){ // remove callback param
        //call to an API
        res = await classifierOne(filename);
        return res; // return value
    }
    var functionTwo = async function(){
        //call to an API
        res = await classifierTwo(filename);
        return res;
    }
    var functionThree = async function(){
        //call to an API
        res = await classifierThree(filename);
        return res;
    }
    var functionFour = async function(){
        //call to an API
        res = await classifierFour(filename);
        return res;
    }

    stack.push(functionOne);
    stack.push(functionTwo);
    stack.push(functionThree);
    stack.push(functionFour);

    async.parallel(stack, function(err, res){
        console.log(res[0]);
    })
}

P/s:我的建议是使用Promise.all for this case.对callback说不。

async function classify(filename) { // Make this function become a async function
    var stack = [];

    var functionOne = async function(){ // remove callback param
        //call to an API
        res = await classifierOne(filename);
        return res; // return value
    }
    var functionTwo = async function(){
        //call to an API
        res = await classifierTwo(filename);
        return res;
    }
    var functionThree = async function(){
        //call to an API
        res = await classifierThree(filename);
        return res;
    }
    var functionFour = async function(){
        //call to an API
        res = await classifierFour(filename);
        return res;
    }

    stack.push(functionOne);
    stack.push(functionTwo);
    stack.push(functionThree);
    stack.push(functionFour);

    const result = await Promise.all(stack); // Wait until all "task" finish
    console.log(result);
    return result; // Return the result of all tasks
}

// Call classify function inner a async function
const result = await classify('your_file_name'); // await keyword
console.log(result);
aydmsdu9

aydmsdu92#

我不太确定async npm库,但在它上面看起来,当你有一个回调函数作为async时,它不工作。如果你尝试使用原生promise,它会工作。事

function classify(filename) {
  var stack = [];

  var functionOne = function(callback) {
    //call to an API
    classifierOne(filename).then(res => callback(null, res));
  };
  var functionTwo = function(callback) {
    classifierTwo(filename).then(res => callback(null, res));
  };
  var functionThree = function(callback) {
    classifierThree(filename).then(res => callback(null, res));
  };
  var functionFour = function(callback) {
    classifierFour(filename).then(res => callback(null, res));
  };

  stack.push(functionOne);
  stack.push(functionTwo);
  stack.push(functionThree);
  stack.push(functionFour);
  async.parallel(stack, function(err, res) {
    console.log(res);
  });
}
ecbunoof

ecbunoof3#

你的方法就像。functionOne错过了返回,没有回调。下面返回["1","2","3","4"]

const async = require('async');

const classifierOne   = async ()=> "1"
const classifierTwo   = async ()=> "2"
const classifierThree = async ()=> "3"
const classifierFour  = async ()=> "4"

const classify = filename => {
    var stack = [];

    var functionOne   = async ()=> classifierOne(filename);
    var functionTwo   = async ()=> classifierTwo(filename);
    var functionThree = async ()=> classifierThree(filename);
    var functionFour  = async ()=> classifierFour(filename);

    stack.push(functionOne);
    stack.push(functionTwo);
    stack.push(functionThree);
    stack.push(functionFour);

    async.parallel(stack, (err, res)=>{
        console.log(res);
    })
}

classify("x");

相关问题