async.parallel()

Another important method in this library is async.parallel(), which lets us send off a lot of statements in parallel, like so:

// async-demo/app-parallell.js

const async = require('async');

function getMessages(fn) {
setTimeout(() => {
fn(null,['mess1', 'mess2', 'mess3']);
}, 3000);
}

function getOrders(fn) {
setTimeout(() => {
fn(null, ['order1', 'order2', 'order3']);
}, 5000);
}

async.parallel([
getMessages,
getOrders
],(error, results) => {
if(error) {
console.log('error', error);
} else {
console.log('results', results);
}
});

What we can see from the previous code is that it allows us to kick off several calls in parallel. We specify the calls in an array that we provide to the async.parallell([]) method. From what you can discern here, the functions we provide take one parameter, fn, which is the callback, for example getOrders(fn) {}.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.249.90