Skip to content

Commit

Permalink
chore(typings): improve toPromise typings
Browse files Browse the repository at this point in the history
Add an overload signature that uses PromiseConstructorLike and
PromiseLike.

Closes #2904
  • Loading branch information
cartant authored and benlesh committed Oct 5, 2017
1 parent 0bf77ea commit 124e231
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/operator/toPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { toPromise as higherOrder } from '../operators/toPromise';
/* tslint:disable:max-line-length */
export function toPromise<T>(this: Observable<T>): Promise<T>;
export function toPromise<T>(this: Observable<T>, PromiseCtor: typeof Promise): Promise<T>;
export function toPromise<T>(this: Observable<T>, PromiseCtor: PromiseConstructorLike): Promise<T>;
/* tslint:enable:max-line-length */

/**
Expand Down Expand Up @@ -55,6 +56,6 @@ export function toPromise<T>(this: Observable<T>, PromiseCtor: typeof Promise):
* @method toPromise
* @owner Observable
*/
export function toPromise<T>(this: Observable<T>, PromiseCtor?: typeof Promise): Promise<T> {
export function toPromise<T>(this: Observable<T>, PromiseCtor?: PromiseConstructorLike): Promise<T> {
return higherOrder(PromiseCtor)(this) as Promise<T>;
}
5 changes: 3 additions & 2 deletions src/operators/toPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UnaryFunction } from '../interfaces';
/* tslint:disable:max-line-length */
export function toPromise<T>(): UnaryFunction<Observable<T>, Promise<T>>;
export function toPromise<T>(PromiseCtor: typeof Promise): UnaryFunction<Observable<T>, Promise<T>>;
export function toPromise<T>(PromiseCtor: PromiseConstructorLike): UnaryFunction<Observable<T>, Promise<T>>;
/* tslint:enable:max-line-length */

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ export function toPromise<T>(PromiseCtor: typeof Promise): UnaryFunction<Observa
* @method toPromise
* @owner Observable
*/
export function toPromise<T>(PromiseCtor?: typeof Promise): UnaryFunction<Observable<T>, Promise<T>> {
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) {
Expand All @@ -73,6 +74,6 @@ export function toPromise<T>(PromiseCtor?: typeof Promise): UnaryFunction<Observ
return new PromiseCtor((resolve, reject) => {
let value: any;
source.subscribe((x: T) => value = x, (err: any) => reject(err), () => resolve(value));
});
}) as Promise<T>;
};
}

0 comments on commit 124e231

Please sign in to comment.