OLD | NEW |
| (Empty) |
1 function MakePromise (asap) { | |
2 function Promise(fn) { | |
3 if (typeof this !== 'object' || typeof fn !== 'function') throw
new TypeError(); | |
4 this._state = null; | |
5 this._value = null; | |
6 this._deferreds = [] | |
7 | |
8 doResolve(fn, resolve.bind(this), reject.bind(this)); | |
9 } | |
10 | |
11 function handle(deferred) { | |
12 var me = this; | |
13 if (this._state === null) { | |
14 this._deferreds.push(deferred); | |
15 return | |
16 } | |
17 asap(function() { | |
18 var cb = me._state ? deferred.onFulfilled : deferred.onR
ejected | |
19 if (typeof cb !== 'function') { | |
20 (me._state ? deferred.resolve : deferred.reject)
(me._value); | |
21 return; | |
22 } | |
23 var ret; | |
24 try { | |
25 ret = cb(me._value); | |
26 } | |
27 catch (e) { | |
28 deferred.reject(e); | |
29 return; | |
30 } | |
31 deferred.resolve(ret); | |
32 }) | |
33 } | |
34 | |
35 function resolve(newValue) { | |
36 try { //Promise Resolution Procedure: https://github.com/promise
s-aplus/promises-spec#the-promise-resolution-procedure | |
37 if (newValue === this) throw new TypeError(); | |
38 if (newValue && (typeof newValue === 'object' || typeof
newValue === 'function')) { | |
39 var then = newValue.then; | |
40 if (typeof then === 'function') { | |
41 doResolve(then.bind(newValue), resolve.b
ind(this), reject.bind(this)); | |
42 return; | |
43 } | |
44 } | |
45 this._state = true; | |
46 this._value = newValue; | |
47 finale.call(this); | |
48 } catch (e) { reject.call(this, e); } | |
49 } | |
50 | |
51 function reject(newValue) { | |
52 this._state = false; | |
53 this._value = newValue; | |
54 finale.call(this); | |
55 } | |
56 | |
57 function finale() { | |
58 for (var i = 0, len = this._deferreds.length; i < len; i++) { | |
59 handle.call(this, this._deferreds[i]); | |
60 } | |
61 this._deferreds = null; | |
62 } | |
63 | |
64 /** | |
65 * Take a potentially misbehaving resolver function and make sure | |
66 * onFulfilled and onRejected are only called once. | |
67 * | |
68 * Makes no guarantees about asynchrony. | |
69 */ | |
70 function doResolve(fn, onFulfilled, onRejected) { | |
71 var done = false; | |
72 try { | |
73 fn(function (value) { | |
74 if (done) return; | |
75 done = true; | |
76 onFulfilled(value); | |
77 }, function (reason) { | |
78 if (done) return; | |
79 done = true; | |
80 onRejected(reason); | |
81 }) | |
82 } catch (ex) { | |
83 if (done) return; | |
84 done = true; | |
85 onRejected(ex); | |
86 } | |
87 } | |
88 | |
89 Promise.prototype['catch'] = function (onRejected) { | |
90 return this.then(null, onRejected); | |
91 }; | |
92 | |
93 Promise.prototype.then = function(onFulfilled, onRejected) { | |
94 var me = this; | |
95 return new Promise(function(resolve, reject) { | |
96 handle.call(me, { | |
97 onFulfilled: onFulfilled, | |
98 onRejected: onRejected, | |
99 resolve: resolve, | |
100 reject: reject | |
101 }); | |
102 }) | |
103 }; | |
104 | |
105 Promise.resolve = function (value) { | |
106 if (value && typeof value === 'object' && value.constructor ===
Promise) { | |
107 return value; | |
108 } | |
109 | |
110 return new Promise(function (resolve) { | |
111 resolve(value); | |
112 }); | |
113 }; | |
114 | |
115 Promise.reject = function (value) { | |
116 return new Promise(function (resolve, reject) { | |
117 reject(value); | |
118 }); | |
119 }; | |
120 | |
121 | |
122 return Promise; | |
123 } | |
124 | |
125 if (typeof module !== 'undefined') { | |
126 module.exports = MakePromise; | |
127 } | |
128 | |
OLD | NEW |