Skip to content

Commit

Permalink
feat(toPromise): now exists as a permanent method on Observable
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Oct 6, 2017
1 parent 16114fb commit 2e49a5c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
25 changes: 25 additions & 0 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,29 @@ export class Observable<T> implements Subscribable<T> {

return pipeFromArray(operations)(this);
}

/* tslint:disable:max-line-length */
toPromise<T>(this: Observable<T>): Promise<T>;
toPromise<T>(this: Observable<T>, PromiseCtor: typeof Promise): Promise<T>;
toPromise<T>(this: Observable<T>, PromiseCtor: PromiseConstructorLike): Promise<T>;
/* 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<T>;
}
}
13 changes: 2 additions & 11 deletions src/add/operator/toPromise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@

import { Observable } from '../../Observable';
import { toPromise } from '../../operator/toPromise';

Observable.prototype.toPromise = toPromise;

declare module '../../Observable' {
interface Observable<T> {
toPromise: typeof toPromise;
}
}
// HACK: does nothing, because `toPromise` now lives on the `Observable` itself.
// leaving this module here to prevent breakage.
3 changes: 1 addition & 2 deletions src/operator/toPromise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Observable } from '../Observable';
import { toPromise as higherOrder } from '../operators/toPromise';

/* tslint:disable:max-line-length */
export function toPromise<T>(this: Observable<T>): Promise<T>;
Expand Down Expand Up @@ -57,5 +56,5 @@ export function toPromise<T>(this: Observable<T>, PromiseCtor: PromiseConstructo
* @owner Observable
*/
export function toPromise<T>(this: Observable<T>, PromiseCtor?: PromiseConstructorLike): Promise<T> {
return higherOrder(PromiseCtor)(this) as Promise<T>;
return this.toPromise();
}
20 changes: 1 addition & 19 deletions src/operators/toPromise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Observable } from '../Observable';
import { root } from '../util/root';
import { UnaryFunction } from '../interfaces';

/* tslint:disable:max-line-length */
Expand Down Expand Up @@ -58,22 +57,5 @@ export function toPromise<T>(PromiseCtor: PromiseConstructorLike): UnaryFunction
* @owner Observable
*/
export function toPromise<T>(PromiseCtor?: PromiseConstructorLike): UnaryFunction<Observable<T>, Promise<T>> {
return (source: Observable<T>) => {
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<T>;
};
return (source: Observable<T>) => source.toPromise();
}

0 comments on commit 2e49a5c

Please sign in to comment.