MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS async js

Parallel : multi-tasking


async.parallel(tasks, afterTasksCallback) will execute a set of tasks in parallel and wait the end of all tasks (reported by the call of callback function).

When tasks are finished, async call the main callback with all errors and all results of tasks.

function shortTimeFunction(callback) {
 setTimeout(function() {
 callback(null, 'resultOfShortTime');
 }, 200);
}
function mediumTimeFunction(callback) {
 setTimeout(function() {
 callback(null, 'resultOfMediumTime');
 }, 500);
}
function longTimeFunction(callback) {
 setTimeout(function() {
 callback(null, 'resultOfLongTime');
 }, 1000);
}
async.parallel([
 shortTimeFunction,
 mediumTimeFunction,
 longTimeFunction
 ],
 function(err, results) {
 if (err) {
 return console.error(err);
 }
 console.log(results);
 });

Result :

["resultOfShortTime", "resultOfMediumTime", "resultOfLongTime"].

Call async.parallel() with an object


You can replace the tasks array parameter by an object. In this case, results will be also an object with the same keys than tasks.

It's very useful to compute some tasks and find easily each result.

async.parallel({
 short: shortTimeFunction,
 medium: mediumTimeFunction,
 long: longTimeFunction
 },
 function(err, results) {
 if (err) {
 return console.error(err);
 }
console.log(results);
 });

Result :

{short: "resultOfShortTime", medium: "resultOfMediumTime", long: "resultOfLongTime"}.

Resolving multiple values


Each parallel function is passed a callback. This callback can either return an error as the first argument or success values after that. If a callback is passed several success values, these results are returned as an array.

async.parallel({
 short: function shortTimeFunction(callback) {
 setTimeout(function() {
 callback(null, 'resultOfShortTime1', 'resultOfShortTime2');
 }, 200);
 },
 medium: function mediumTimeFunction(callback) {
 setTimeout(function() {
 callback(null, 'resultOfMediumTime1', 'resultOfMeiumTime2');
 }, 500);
 }
 },
 function(err, results) {
 if (err) {
 return console.error(err);
 }
 console.log(results);
 });

Result :


{
 short: ["resultOfShortTime1", "resultOfShortTime2"],
 medium: ["resultOfMediumTime1", "resultOfMediumTime2"]
}


Conclusion

In this page (written and validated by ) you learned about NodeJS async js . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJs File upload.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.