@@ -18,8 +18,6 @@ const PENDING = void 0;
18
18
const FULFILLED = 1 ;
19
19
const REJECTED = 2 ;
20
20
21
- const TRY_CATCH_ERROR = { error : null } ;
22
-
23
21
function selfFulfillment ( ) {
24
22
return new TypeError ( "You cannot resolve a promise with itself" ) ;
25
23
}
@@ -28,15 +26,6 @@ function cannotReturnOwn() {
28
26
return new TypeError ( 'A promises callback cannot return that same promise.' ) ;
29
27
}
30
28
31
- function getThen ( promise ) {
32
- try {
33
- return promise . then ;
34
- } catch ( error ) {
35
- TRY_CATCH_ERROR . error = error ;
36
- return TRY_CATCH_ERROR ;
37
- }
38
- }
39
-
40
29
function tryThen ( then , value , fulfillmentHandler , rejectionHandler ) {
41
30
try {
42
31
then . call ( value , fulfillmentHandler , rejectionHandler ) ;
@@ -47,8 +36,8 @@ function tryThen(then, value, fulfillmentHandler, rejectionHandler) {
47
36
48
37
function handleForeignThenable ( promise , thenable , then ) {
49
38
asap ( promise => {
50
- var sealed = false ;
51
- var error = tryThen ( then , thenable , value => {
39
+ let sealed = false ;
40
+ let error = tryThen ( then , thenable , value => {
52
41
if ( sealed ) { return ; }
53
42
sealed = true ;
54
43
if ( thenable !== value ) {
@@ -87,10 +76,7 @@ function handleMaybeThenable(promise, maybeThenable, then) {
87
76
maybeThenable . constructor . resolve === originalResolve ) {
88
77
handleOwnThenable ( promise , maybeThenable ) ;
89
78
} else {
90
- if ( then === TRY_CATCH_ERROR ) {
91
- reject ( promise , TRY_CATCH_ERROR . error ) ;
92
- TRY_CATCH_ERROR . error = null ;
93
- } else if ( then === undefined ) {
79
+ if ( then === undefined ) {
94
80
fulfill ( promise , maybeThenable ) ;
95
81
} else if ( isFunction ( then ) ) {
96
82
handleForeignThenable ( promise , maybeThenable , then ) ;
@@ -104,7 +90,14 @@ function resolve(promise, value) {
104
90
if ( promise === value ) {
105
91
reject ( promise , selfFulfillment ( ) ) ;
106
92
} else if ( objectOrFunction ( value ) ) {
107
- handleMaybeThenable ( promise , value , getThen ( value ) ) ;
93
+ let then ;
94
+ try {
95
+ then = value . then ;
96
+ } catch ( error ) {
97
+ reject ( promise , error ) ;
98
+ return ;
99
+ }
100
+ handleMaybeThenable ( promise , value , then ) ;
108
101
} else {
109
102
fulfill ( promise , value ) ;
110
103
}
@@ -174,46 +167,31 @@ function publish(promise) {
174
167
promise . _subscribers . length = 0 ;
175
168
}
176
169
177
-
178
- function tryCatch ( callback , detail ) {
179
- try {
180
- return callback ( detail ) ;
181
- } catch ( e ) {
182
- TRY_CATCH_ERROR . error = e ;
183
- return TRY_CATCH_ERROR ;
184
- }
185
- }
186
-
187
170
function invokeCallback ( settled , promise , callback , detail ) {
188
171
let hasCallback = isFunction ( callback ) ,
189
- value , error , succeeded , failed ;
172
+ value , error , succeeded = true ;
190
173
191
174
if ( hasCallback ) {
192
- value = tryCatch ( callback , detail ) ;
193
-
194
- if ( value === TRY_CATCH_ERROR ) {
195
- failed = true ;
196
- error = value . error ;
197
- value . error = null ;
198
- } else {
199
- succeeded = true ;
175
+ try {
176
+ value = callback ( detail ) ;
177
+ } catch ( e ) {
178
+ succeeded = false ;
179
+ error = e ;
200
180
}
201
181
202
182
if ( promise === value ) {
203
183
reject ( promise , cannotReturnOwn ( ) ) ;
204
184
return ;
205
185
}
206
-
207
186
} else {
208
187
value = detail ;
209
- succeeded = true ;
210
188
}
211
189
212
190
if ( promise . _state !== PENDING ) {
213
191
// noop
214
192
} else if ( hasCallback && succeeded ) {
215
193
resolve ( promise , value ) ;
216
- } else if ( failed ) {
194
+ } else if ( succeeded === false ) {
217
195
reject ( promise , error ) ;
218
196
} else if ( settled === FULFILLED ) {
219
197
fulfill ( promise , value ) ;
@@ -249,7 +227,6 @@ function makePromise(promise) {
249
227
export {
250
228
nextId ,
251
229
makePromise ,
252
- getThen ,
253
230
noop ,
254
231
resolve ,
255
232
reject ,
0 commit comments