diff --git a/src/Observable.ts b/src/Observable.ts index 41ab5faf5c..d09810c843 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -327,4 +327,29 @@ export class Observable implements Subscribable { return pipeFromArray(operations)(this); } + + /* tslint:disable:max-line-length */ + toPromise(this: Observable): Promise; + toPromise(this: Observable, PromiseCtor: typeof Promise): Promise; + toPromise(this: Observable, PromiseCtor: PromiseConstructorLike): Promise; + /* tslint:enable:max-line-length */ + + toPromise(PromiseCtor?: PromiseConstructorLike) { + if (!PromiseCtor) { + if (root.Rx && root.Rx.config && root.Rx.config.Promise) { + PromiseCtor = root.Rx.config.Promise; + } else if (root.Promise) { + PromiseCtor = root.Promise; + } + } + + if (!PromiseCtor) { + throw new Error('no Promise impl found'); + } + + return new PromiseCtor((resolve, reject) => { + let value: any; + this.subscribe((x: T) => value = x, (err: any) => reject(err), () => resolve(value)); + }) as Promise; + } } diff --git a/src/add/operator/toPromise.ts b/src/add/operator/toPromise.ts index a24bc0c4ad..9f3002f2bb 100644 --- a/src/add/operator/toPromise.ts +++ b/src/add/operator/toPromise.ts @@ -1,11 +1,2 @@ - -import { Observable } from '../../Observable'; -import { toPromise } from '../../operator/toPromise'; - -Observable.prototype.toPromise = toPromise; - -declare module '../../Observable' { - interface Observable { - toPromise: typeof toPromise; - } -} \ No newline at end of file +// HACK: does nothing, because `toPromise` now lives on the `Observable` itself. +// leaving this module here to prevent breakage. diff --git a/src/operator/toPromise.ts b/src/operator/toPromise.ts index 7fe0fe16f7..7e16109216 100644 --- a/src/operator/toPromise.ts +++ b/src/operator/toPromise.ts @@ -1,5 +1,4 @@ import { Observable } from '../Observable'; -import { toPromise as higherOrder } from '../operators/toPromise'; /* tslint:disable:max-line-length */ export function toPromise(this: Observable): Promise; @@ -57,5 +56,5 @@ export function toPromise(this: Observable, PromiseCtor: PromiseConstructo * @owner Observable */ export function toPromise(this: Observable, PromiseCtor?: PromiseConstructorLike): Promise { - return higherOrder(PromiseCtor)(this) as Promise; + return this.toPromise(); } diff --git a/src/operators/toPromise.ts b/src/operators/toPromise.ts index fc1996647b..ffd6ea5b8d 100644 --- a/src/operators/toPromise.ts +++ b/src/operators/toPromise.ts @@ -1,5 +1,4 @@ import { Observable } from '../Observable'; -import { root } from '../util/root'; import { UnaryFunction } from '../interfaces'; /* tslint:disable:max-line-length */ @@ -58,22 +57,5 @@ export function toPromise(PromiseCtor: PromiseConstructorLike): UnaryFunction * @owner Observable */ export function toPromise(PromiseCtor?: PromiseConstructorLike): UnaryFunction, Promise> { - return (source: Observable) => { - if (!PromiseCtor) { - if (root.Rx && root.Rx.config && root.Rx.config.Promise) { - PromiseCtor = root.Rx.config.Promise; - } else if (root.Promise) { - PromiseCtor = root.Promise; - } - } - - if (!PromiseCtor) { - throw new Error('no Promise impl found'); - } - - return new PromiseCtor((resolve, reject) => { - let value: any; - source.subscribe((x: T) => value = x, (err: any) => reject(err), () => resolve(value)); - }) as Promise; - }; + return (source: Observable) => source.toPromise(); }