3

I have a problem with async.parallel in node js. Following code ist working perfect

  var data = {};

  data.task_1= function(callback) { 
    var res_1 = "hello 1";
    callback(null, res_1);
  }
  data.task_2 = function(callback) { 
    var res_2 = "hello 2";
    callback(null, res_2);
  }

  async.parallel(data, function(err, results) {
    if (err){
      console.log(err);
    }
    console.log(results);
  });

The result is: { task_1: 'hello 1', task_2: 'hello 2' }

But if I try to do the task with a function calling data from a database, I become following error: TypeError: callback is not a function and the result { task_2: 'hello 2', task_1: undefined }

I see in the log, that the data is retrieved before the json file is logged.

Here is my code:

  var data = {};

  data.task_1 = async function(callback) { 
    var res_1 = await getData("xyz");
    console.log(res_1);
    callback(null, res_1);
  }
  data.task_2 = function(callback) { 
    var res_2 = "hello 2";
    callback(null, res_2);
  }

  async.parallel(data, function(err, results) {
    if (err){
      console.log(err);
    }
    console.log(results);
  });

What do I miss? Thanks for any help!

0

1 Answer 1

1

You could return the result from getData(), this should work (according to the docs): https://caolan.github.io/async/v3/global.html#AsyncFunction, and you can use the await statement too:

var data = {};

data.task_1 = async function() { 
    var res_1 = await getData("xyz");
    console.log(res_1);
    return res_1;
}
data.task_2 = function(callback) { 
    var res_2 = "hello 2";
    callback(null, res_2);
}

async.parallel(data, function(err, results) {
    if (err){
        console.log(err);
    }
    console.log(results);
});

I believe it's because if we mark the function as async, no callback will be passed. So if I assign a non-async function and use .then(), the callback will be passed and all will work nicely! From the docs: "Wherever we accept a Node-style async function, we also directly accept an ES2017 async function. In this case, the async function will not be passed a final callback argument" Which explaints the error message "callback is not a function."

Sign up to request clarification or add additional context in comments.

8 Comments

how's that any different from using async/await?
@SoulKa: It might not be, from your perspective. Personally, I like the continuation-passing style they're using here.
Perfect! Thanks a lot. It works, but I do not see the difference to async/await either...
I believe it's because if we mark the function as async, no callback will be passed. So if I assign a non-async function and use .then(), the callback will be passed and all will work nicely! From the docs: "Wherever we accept a Node-style async function, we also directly accept an ES2017 async function. In this case, the async function will not be passed a final callback argument" Which explaints the error message "callback is not a function."
I wanted to clarify since the question is phrased in terms of await
|

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.