1 | 'use strict';
|
2 |
|
3 | var Promise = global.Promise;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | module.exports.promise = function (fn, context, args) {
|
11 |
|
12 | if (!Array.isArray(args)) {
|
13 | args = Array.prototype.slice.call(args);
|
14 | }
|
15 |
|
16 | if (typeof fn !== 'function') {
|
17 | return Promise.reject(new Error('fn must be a function'));
|
18 | }
|
19 |
|
20 | return new Promise(function(resolve, reject) {
|
21 | args.push(function(err, data) {
|
22 | if (err) {
|
23 | reject(err);
|
24 | } else {
|
25 | resolve(data);
|
26 | }
|
27 | });
|
28 |
|
29 | fn.apply(context, args);
|
30 | });
|
31 | };
|
32 |
|
33 |
|
34 | module.exports.reject = function (err) {
|
35 | return Promise.reject(err);
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 | module.exports.use = function(promise) {
|
41 | Promise = promise;
|
42 | };
|