forked from cujojs/when
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.js
43 lines (37 loc) · 1.16 KB
/
sequence.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* sequence.js
*
* Run a set of task functions in sequence. All tasks will
* receive the same args.
*
* @author [email protected]
*/
(function(define) {
define(['./when'], function(when) {
/**
* Run array of tasks in sequence with no overlap
* @param tasks {Array|Promise} array or promiseForArray of task functions
* @param [args] {*} arguments to be passed to all tasks
* @return {Promise} promise for an array containing
* the result of each task in the array position corresponding
* to position of the task in the tasks array
*/
return function sequence(tasks /*, args... */) {
var args = Array.prototype.slice.call(arguments, 1);
return when.reduce(tasks, function(results, task) {
return when(task.apply(null, args), function(result) {
results.push(result);
return results;
});
}, []);
};
});
})(typeof define == 'function' && define.amd
? define
: function (deps, factory) { typeof exports == 'object'
? (module.exports = factory(require('./when')))
: (this.when_sequence = factory(this.when));
}
// Boilerplate for AMD, Node, and browser global
);