@@ -243,18 +243,6 @@ function $$QProvider() {
243
243
*/
244
244
function qFactory ( nextTick , exceptionHandler ) {
245
245
var $qMinErr = minErr ( '$q' , TypeError ) ;
246
- function callOnce ( self , resolveFn , rejectFn ) {
247
- var called = false ;
248
- function wrap ( fn ) {
249
- return function ( value ) {
250
- if ( called ) return ;
251
- called = true ;
252
- fn . call ( self , value ) ;
253
- } ;
254
- }
255
-
256
- return [ wrap ( resolveFn ) , wrap ( rejectFn ) ] ;
257
- }
258
246
259
247
/**
260
248
* @ngdoc method
@@ -361,21 +349,42 @@ function qFactory(nextTick, exceptionHandler) {
361
349
} ,
362
350
363
351
$$resolve : function ( val ) {
364
- var then , fns ;
352
+ var then ;
353
+ try {
354
+ then = ( isObject ( val ) || isFunction ( val ) ) && isFunction ( then = val . then ) ? then : false ;
355
+ } catch ( e ) {
356
+ this . $$reject ( e ) ;
357
+ exceptionHandler ( e ) ;
358
+ return ;
359
+ }
365
360
366
- fns = callOnce ( this , this . $$resolve , this . $$reject ) ;
361
+ if ( then ) {
362
+ this . promise . $$state . status = - 1 ;
363
+ this . $$delayResolve ( val , then ) ;
364
+ } else {
365
+ this . promise . $$state . value = val ;
366
+ this . promise . $$state . status = 1 ;
367
+ scheduleProcessQueue ( this . promise . $$state ) ;
368
+ }
369
+ } ,
370
+
371
+ $$delayResolve : function ( promiseLike , then ) {
372
+ var that = this ;
373
+ var done = false ;
374
+ function resolvePromise ( val ) {
375
+ if ( done ) return ;
376
+ done = true ;
377
+ that . $$resolve ( val ) ;
378
+ }
379
+ function rejectPromise ( val ) {
380
+ if ( done ) return ;
381
+ done = true ;
382
+ that . $$reject ( val ) ;
383
+ }
367
384
try {
368
- if ( ( isObject ( val ) || isFunction ( val ) ) ) then = val && val . then ;
369
- if ( isFunction ( then ) ) {
370
- this . promise . $$state . status = - 1 ;
371
- then . call ( val , fns [ 0 ] , fns [ 1 ] , this . notify ) ;
372
- } else {
373
- this . promise . $$state . value = val ;
374
- this . promise . $$state . status = 1 ;
375
- scheduleProcessQueue ( this . promise . $$state ) ;
376
- }
385
+ then . call ( promiseLike , resolvePromise , rejectPromise , this . notify ) ;
377
386
} catch ( e ) {
378
- fns [ 1 ] ( e ) ;
387
+ rejectPromise ( e ) ;
379
388
exceptionHandler ( e ) ;
380
389
}
381
390
} ,
0 commit comments