1 | var _ = require('./_');
|
2 | var isFn = _.isFunction;
|
3 |
|
4 | module.exports = function (fn, self) {
|
5 | return function (a, b, c, d, e) {
|
6 | var len = arguments.length
|
7 | , args, promise, resolve, reject;
|
8 |
|
9 | promise = new _.Promise(function (r, rj) {
|
10 | resolve = r;
|
11 | reject = rj;
|
12 | });
|
13 |
|
14 | function cb (err, val) {
|
15 | err == null ? resolve(val) : reject(err);
|
16 | }
|
17 |
|
18 |
|
19 | switch (len) {
|
20 | case 0: fn.call(self, cb); break;
|
21 | case 1: isFn(a) ? fn.call(self, a) : fn.call(self, a, cb); break;
|
22 | case 2: isFn(b) ? fn.call(self, a, b) : fn.call(self, a, b, cb); break;
|
23 | case 3: isFn(c) ? fn.call(self, a, b, c) : fn.call(self, a, b, c, cb); break;
|
24 | case 4: isFn(d) ? fn.call(self, a, b, c, d) : fn.call(self, a, b, c, d, cb); break;
|
25 | case 5: isFn(e) ? fn.call(self, a, b, c, d, e) : fn.call(self, a, b, c, d, e, cb); break;
|
26 | default:
|
27 | args = new Array(len);
|
28 |
|
29 | for (var i = 0; i < len; i++) {
|
30 | args[i] = arguments[i];
|
31 | }
|
32 |
|
33 | if (isFn(args[len - 1])) {
|
34 | return fn.apply(self, args);
|
35 | }
|
36 |
|
37 | args[i] = cb;
|
38 | fn.apply(self, args);
|
39 | }
|
40 |
|
41 | return promise;
|
42 | };
|
43 | };
|