@@ -6,8 +6,6 @@ import {root} from './util/root';
6
6
import { CoreOperators } from './CoreOperators' ;
7
7
import { SymbolShim } from './util/SymbolShim' ;
8
8
import { toSubscriber } from './util/toSubscriber' ;
9
- import { tryCatch } from './util/tryCatch' ;
10
- import { errorObject } from './util/errorObject' ;
11
9
12
10
import { combineLatestStatic } from './operator/combineLatest' ;
13
11
import { concatStatic } from './operator/concat' ;
@@ -230,13 +228,27 @@ export class Observable<T> implements CoreOperators<T> {
230
228
throw new Error ( 'no Promise impl found' ) ;
231
229
}
232
230
233
- const source = this ;
234
-
235
231
return new PromiseCtor < void > ( ( resolve , reject ) => {
236
- source . subscribe ( ( value : T ) => {
237
- const result : any = tryCatch ( next ) ( value ) ;
238
- if ( result === errorObject ) {
239
- reject ( errorObject . e ) ;
232
+ const subscription = this . subscribe ( ( value ) => {
233
+ if ( subscription ) {
234
+ // if there is a subscription, then we can surmise
235
+ // the next handling is asynchronous. Any errors thrown
236
+ // need to be rejected explicitly and unsubscribe must be
237
+ // called manually
238
+ try {
239
+ next ( value ) ;
240
+ } catch ( err ) {
241
+ reject ( err ) ;
242
+ subscription . unsubscribe ( ) ;
243
+ }
244
+ } else {
245
+ // if there is NO subscription, then we're getting a nexted
246
+ // value synchronously during subscription. We can just call it.
247
+ // If it errors, Observable's `subscribe` imple will ensure the
248
+ // unsubscription logic is called, then synchronously rethrow the error.
249
+ // After that, Promise will trap the error and send it
250
+ // down the rejection path.
251
+ next ( value ) ;
240
252
}
241
253
} , reject , resolve ) ;
242
254
} ) ;
@@ -369,4 +381,4 @@ export class Observable<T> implements CoreOperators<T> {
369
381
[ SymbolShim . observable ] ( ) {
370
382
return this ;
371
383
}
372
- }
384
+ }
0 commit comments