forked from kriszyp/promised-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.js
42 lines (36 loc) · 951 Bytes
/
step.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
42
'use strict';
(function(define){
define(function(require,exports){
// Vladimir Dronnikov
// inspired by creationix's Step
var when = require('./promise').when;
// execute sequentially functions taken from steps[]
// each successive is fed with the result of prior
// each function can return an immediate value, a promise, or just throw
// in the latter case the next function will receive Error object
// return "undefined" to full stop.
//
// "context" is available to each steps as "this"
//
exports.Step = function(context, steps) {
var next;
next = function() {
var fn, result;
if (!steps.length) {
return arguments[0];
}
fn = steps.shift();
try {
result = fn.apply(context, arguments);
if (result !== void 0) {
result = when(result, next, next);
}
} catch (err) {
next(err);
}
return result;
};
return next();
};
});
})(typeof define!="undefined"?define:function(factory){factory(require,exports)});