1 | /** @license MIT License (c) copyright 2011-2013 original author or authors */
|
2 |
|
3 | /**
|
4 | * parallel.js
|
5 | *
|
6 | * Run a set of task functions in parallel. All tasks will
|
7 | * receive the same args
|
8 | *
|
9 | * @author Brian Cavalier
|
10 | * @author John Hann
|
11 | */
|
12 |
|
13 | (function(define) {
|
14 | define(function(require) {
|
15 |
|
16 | var when = require('./when');
|
17 | var all = when.Promise.all;
|
18 | var slice = Array.prototype.slice;
|
19 |
|
20 | /**
|
21 | * Run array of tasks in parallel
|
22 | * @param tasks {Array|Promise} array or promiseForArray of task functions
|
23 | * @param [args] {*} arguments to be passed to all tasks
|
24 | * @return {Promise} promise for array containing the
|
25 | * result of each task in the array position corresponding
|
26 | * to position of the task in the tasks array
|
27 | */
|
28 | return function parallel(tasks /*, args... */) {
|
29 | return all(slice.call(arguments, 1)).then(function(args) {
|
30 | return when.map(tasks, function(task) {
|
31 | return task.apply(void 0, args);
|
32 | });
|
33 | });
|
34 | };
|
35 |
|
36 | });
|
37 | })(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });
|
38 |
|
39 |
|