forked from Emerge-History/morphkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
38 lines (35 loc) · 1022 Bytes
/
promise.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
function promissive() {
var args = Array.prototype.slice.call(arguments);
var func = args.shift();
return new Promise(function (res, rej) {
var cb = function () {
var cb_args = Array.prototype.slice.call(arguments);
var err = cb_args.shift();
if (err) {
return rej(err);
} else {
if (cb_args.length == 1) {
return res(cb_args[0]);
} else {
return res(cb_args);
}
}
};
args.push(cb);
func.apply(null, args);
});
}
function expand(func) {
return function (res) {
res = Array.isArray(res) ? res : [res];
return func.apply(null, res);
};
}
Function.prototype.promise = function () {
var _this = this;
var args = Array.prototype.slice.call(arguments);
args.unshift(_this);
return promissive.apply(null, args);
}
module.exports = promissive;
module.exports.expand = expand;