0

Running the code bellow I just get "undefined". It seems like the async.parallel call executes the final function before the 4 parallel functions are finished.

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]);
    })
}
1
  • try using await Promise.all(stack) @Marc B. Commented Dec 26, 2019 at 15:58

3 Answers 3

2

If you are using async with version 3.x, we have a big change - Support async/await , with the version, with tasks which have been wrap to a Promise (when you use async/await keyword) then the callback will be disable, you need to return value in the tasks instead of call callback(null, value).

This mean, to fix your case, the code will come to:

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: My suggestion is using Promise.all for this case. Say no to 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);
Sign up to request clarification or add additional context in comments.

2 Comments

THis works and res[0] will be printed. But I want the whole function to return the result after all finished. Something like: ... answer = await async.parallel(stack, function(err, res){ return res; }) return answer; ... Useing this code the return will just be undefined.
It's because classifierFour etc is not defined in the code. also there is no need for res, you return directly, see my answer below.
0

I am not too sure about the async npm library but on top of it looks like it doesn't work when you have a callback function as async. If you try it using native promises, it will work. Something like

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);
  });
}

Comments

0

Your methods like i.e. functionOne missed the return and didn't a callback. The below returns ["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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.