import { Observable } from '../Observable'; import { root } from '../util/root'; /* tslint:disable:max-line-length */ export function toPromise(this: Observable): Promise; export function toPromise(this: Observable, PromiseCtor: typeof Promise): Promise; /* tslint:disable:max-line-length */ /** * @param PromiseCtor * @return {Promise} * @method toPromise * @owner Observable */ export function toPromise(this: Observable, PromiseCtor?: typeof Promise): Promise { 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)); }); }