0

Hello i am using async parallel in my code for practice code

var tasklist = [],tempjson={};
for(var i = 0; i < 10; i++)  {
   tempjson.data = 'i is' + i;
   for(var j = 0; j < 20; j++){
      tasklist.push(doSomething(tempjson,j));
   }
}
 async.parallel(taskList, function(err, data) {
       console.log(data);
 });

function doSomething(params,j){
       params.data2 = 'j value is'+ j
       return callback(null,params)
}

want to do something like that but i am getting error that callback is undefined can somebody help me what is wrong with code

2 Answers 2

1

You are not passing callback as a argument to the function doSomething.

Just add the callback argument like

function doSomething(params,j){
     return function(callback){
       params.data2 = 'j value is'+ j
       return callback(null,params)
     }   

}

This will solve the problem!!

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

3 Comments

getting error stack: 'TypeError: callback is not a function\n
Updated the answer!!, Sorry my bad
thax bro for help
1

ReferenceError: callback is not defined

This is because you're not passing callback to doSomething(). As per docs, We need to pass callback to the function.

Also, there is a typo in your code. It is tasklist and not taskList

Fixing this,

  var tasklist = [], tempjson = {};
  for (var i = 0; i < 10; i++) {
    tempjson.data = 'i is' + i;
    for (var j = 0; j < 20; j++) {
      tasklist.push(function (callback) {
        doSomething(tempjson, j, callback);
      }); // Array of task as per docs
    }
  }
  async.parallel(tasklist, function (err, data) {
    console.log(data);
  });

  function doSomething(params, j, callback) { // passing callback as per docs
    params.data2 = 'j value is' + j;
    return callback(null, params)
  }

This will log

[ { data: 'i is9', data2: 'j value is20' },
{ data: 'i is9', data2: 'j value is20' },
... ... 100 more items ]

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.