if (!PromiseCtor) { thrownewError('no Promise impl found'); }
returnnewPromiseCtor<void>((resolve, reject) => { // Must be declared in a separate statement to avoid a RefernceError when // accessing subscription below in the closure due to Temporal Dead Zone. letsubscription: Subscription; subscription = this.subscribe((value) => { if (subscription) { // if there is a subscription, then we can surmise // the next handling is asynchronous. Any errors thrown // need to be rejected explicitly and unsubscribe must be // called manually try { next(value); } catch (err) { reject(err); subscription.unsubscribe(); } } else { // if there is NO subscription, then we're getting a nexted // value synchronously during subscription. We can just call it. // If it errors, Observable's `subscribe` will ensure the // unsubscription logic is called, then synchronously rethrow the error. // After that, Promise will trap the error and send it // down the rejection path. next(value); } }, reject, resolve); }); }