How to use the parallel function from async

Find comprehensive JavaScript async.parallel code examples handpicked from public code repositorys.

async.parallel is a function in the Async.js library for Node.js that allows multiple asynchronous functions to be executed in parallel.

129
130
131
132
133
134
135
136
137
138
    .prompt(questions)
    .then((answers) => callback({ ...answers, ...prefilled }));
}

function checkAllDependencies(callback) {
  async.parallel(
    async.reflectAll([checkPrimaryDependencies, checkSecondaryDependencies]),
    (error, results) => {
      const errors = results.map((result) => result.error).filter(Boolean);
      // all errors will be the same
fork icon268
star icon0
watch icon1

43
44
45
46
47
48
49
50
51
52
            from.sourceUrl = result.url;
            callback(null, from);
        })
    }
});
async.parallel(async.reflectAll(tasks), (err, results) => {
    const result = results.find(x => !x.error);
    if (result) {
        cb(null, result.value);
    } else {
fork icon51
star icon590
watch icon0

How does async.parallel work?

async.parallel is a function that takes an object or an array of asynchronous functions to execute. It runs all the functions in parallel and calls the final callback function when all the tasks have completed.

If an object of tasks is passed, the result is an object with the same keys and corresponding values, where each value is the result of the corresponding task. If an array of tasks is passed, the result is an array of the results of each task in the order they were specified.

The syntax for using async.parallel is:

csharp
async.parallel(tasks, callback);

Here, tasks is an object or array of functions to execute in parallel and callback is the final callback function to be called once all the tasks have completed. The callback function takes two arguments: an error object and the result of the executed tasks.

async.parallel returns a Promise if no callback function is passed in.

79
80
81
82
83
84
85
86
87
88
  // This file should become a blog post or page!
  if (!isPublic(path) && !isTemplate(path) && !is_preview) {
    queue.buildAndSet = buildAndSet.bind(this, blog, path, options);
  }

  async.parallel(queue, function (err) {
    if (err) return callback(err);
    rebuildDependents(blog.id, path, callback);
  });
});
fork icon69
star icon0
watch icon23

178
179
180
181
182
183
184
185
186
187
});

// functions will be executed by random order
var funcs = {
    'async': function(callback) {
        async.parallel(tasks, callback);
    },
    'neo-async': function(callback) {
        neo_async.parallel(tasks, callback);
    }
fork icon45
star icon0
watch icon0

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const async = require("async");

async.parallel(
  [
    function (callback) {
      setTimeout(function () {
        console.log("Task 1 is done");
        callback(null, "result 1");
      }, 1000);
    },
    function (callback) {
      setTimeout(function () {
        console.log("Task 2 is done");
        callback(null, "result 2");
      }, 2000);
    },
  ],
  function (err, results) {
    console.log("All tasks are done");
    console.log(results);
  }
);

In this example, async.parallel is passed an array of two functions. Each function uses setTimeout to simulate an asynchronous task that takes one or two seconds to complete. When both tasks are complete, the final callback function is called with the error object (which is null in this example) and an array of the results of each task, which are logged to the console. The output of the example will be: bash Copy code

100
101
102
103
104
105
106
107
108
109
function worker(task, cb){
  if (!task.collection.items || !task.collection.items.length){
    return cb();
  }

  async.parallel([
    function(parallel_cb){
      if (callbackWrapper.isDone){
        return parallel_cb();
      }
fork icon44
star icon0
watch icon2

+ 3 other calls in file

147
148
149
150
151
152
153
154
155
156
var me = this;

var sources = me.getSources();
var sourceEntries = me.getSourceEntries(sources);

Async.parallel(
    Async.reflectAll(sourceEntries),
    function(err, allResults) {
        if (err) {
            exitCallback(err);
fork icon3
star icon7
watch icon2

81
82
83
84
85
86
87
88
89
90
    })
  }
  jobs.push(job)
}
return new Jayson.Method(function (args, done) {
  async.parallel(async.reflectAll(jobs),
    function (err, results) {
      done(err, results)
    })
})
fork icon2
star icon2
watch icon4

204
205
206
207
208
209
210
211
212
213

      errorResults.forEach((error) => onError && onError(error));
      resolve(successResults);
    }

    async.parallel(wrappedTasks, callback);
  }),

asyncMap: (array, fn, onError) => {
  assert(array && fn && onError, 'missing parameters');
fork icon19
star icon0
watch icon0

500
501
502
503
504
505
506
507
508
509
if (typeof options === 'function') {
    callback = options;
    options = {};
}

return Async.parallel({
    refreshToken: Async.apply(this._refreshTokenIfNeeded.bind(this), auth),
    convertAttachments: (callback) => {

        if (!params.attachments || params.attachments.length === 0) {
fork icon2
star icon1
watch icon6

831
832
833
834
835
836
837
838
839
840
* when the continuation is called.
* @returns {Function} the partially-applied function
* @example
*
* // using apply
* async.parallel([
*     async.apply(fs.writeFile, 'testfile1', 'test1'),
*     async.apply(fs.writeFile, 'testfile2', 'test2')
* ]);
*
fork icon0
star icon2
watch icon1

+ 15 other calls in file

168
169
170
171
172
173
174
175
176
177
                parallel_callback(err);
            });
    });
};

async.parallel(parallelTasks, (err) => {
    if (err) {
        helper.response.onError(err, callback);
        return;
    }
fork icon0
star icon0
watch icon1

432
433
434
435
436
437
438
439
440
441
            parallel_callback(__err);
        });
    });
});

async.parallel(parallelTasks, (_err) => {
    if (_err) {
        helper.response.onError('error: getFramesWithOverlay', callback);
        return;
    }
fork icon0
star icon0
watch icon1

+ 2 other calls in file

355
356
357
358
359
360
361
362
363
364
var selectDate = $("#selectDate");
const scanCancel = $("#scanCancel");
var intvalWorker;

var loadAllData = function (callbackLoad) {
  async.parallel({
    pointSop: function (callback) {
      console.log(config.apiHost + 'api/pointSop/' + currentCust + '/' + pointPanel.currentPointid + '/' + pointPanel.pointEmpid + '?selectdate=' + pointPanel.currentDate);
      request(config.apiHost + 'api/pointSop/' + currentCust + '/' + pointPanel.currentPointid + '/' + pointPanel.pointEmpid + '?selectdate=' + pointPanel.currentDate, function (error, response, body) {
        if (error == null) {
fork icon0
star icon0
watch icon2

+ 2 other calls in file

14
15
16
17
18
19
20
21
22
23
24
25
const mongodbb = 'mongodb://localhost:27017/local_library'


mongoose.connect(mongodbb, { useNewUrlParser: true, useUnifiedTopology: true })


exports.index = (req, res) => { 
  async.parallel
  // eslint-disable-next-line indent, no-unexpected-multiline
(
    {
    book_count (callback) {
fork icon0
star icon0
watch icon1

+ 13 other calls in file