Skip to content

Commit

Permalink
feat: toPromise removed. (#6598)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `toPromise` has been removed from `Observable`. As a workaround, use `firstValueFrom` and `lastValueFrom` functions, which can be imported from `rxjs`. For example `source$.toPromise()` would be `lastValueFrom(source$)`. In the case that you know your source only has one value, we recommend using `firstValueFrom(source$)`.
  • Loading branch information
benlesh committed Sep 19, 2021
1 parent 79f871a commit 8233648
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 88 deletions.
3 changes: 0 additions & 3 deletions api_guard/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,6 @@ export declare class Observable<T> implements Subscribable<T> {
subscribe(observer?: Partial<Observer<T>>): Subscription;
subscribe(next: (value: T) => void): Subscription;
subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Subscription;
toPromise(): Promise<T | undefined>;
toPromise(PromiseCtor: typeof Promise): Promise<T | undefined>;
toPromise(PromiseCtor: PromiseConstructorLike): Promise<T | undefined>;
static create: (...args: any[]) => any;
}

Expand Down
4 changes: 0 additions & 4 deletions spec-dtslint/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ describe('pipe', () => {
const customOperator = () => <T>(a: Observable<T>) => a;
const o = of('foo').pipe(customOperator()); // $ExpectType Observable<string>
});

it('should have proper return type for toPromise', () => {
const o = of('foo').toPromise(); // $ExpectType Promise<string | undefined>
});
});

describe('subscribe', () => {
Expand Down
36 changes: 0 additions & 36 deletions spec/operators/toPromise-spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions spec/util/isPromise-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ describe('isPromise', () => {
expect(isPromise(o)).to.be.true;
});

it('should return true for a Promise that comes from an Observable', () => {
const o: any = of(null).toPromise();
expect(isPromise(o)).to.be.true;
});

it('should NOT return true for any Observable', () => {
const o: any = of(null);

Expand Down
40 changes: 0 additions & 40 deletions src/internal/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,46 +437,6 @@ export class Observable<T> implements Subscribable<T> {
pipe(...operations: OperatorFunction<any, any>[]): Observable<any> {
return pipeFromArray(operations)(this);
}

/* tslint:disable:max-line-length */
/** @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise */
toPromise(): Promise<T | undefined>;
/** @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise */
toPromise(PromiseCtor: typeof Promise): Promise<T | undefined>;
/** @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise */
toPromise(PromiseCtor: PromiseConstructorLike): Promise<T | undefined>;
/* tslint:enable:max-line-length */

/**
* Subscribe to this Observable and get a Promise resolving on
* `complete` with the last emission (if any).
*
* **WARNING**: Only use this with observables you *know* will complete. If the source
* observable does not complete, you will end up with a promise that is hung up, and
* potentially all of the state of an async function hanging out in memory. To avoid
* this situation, look into adding something like {@link timeout}, {@link take},
* {@link takeWhile}, or {@link takeUntil} amongst others.
*
* @method toPromise
* @param [promiseCtor] a constructor function used to instantiate
* the Promise
* @return A Promise that resolves with the last value emit, or
* rejects on an error. If there were no emissions, Promise
* resolves with undefined.
* @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise
*/
toPromise(promiseCtor?: PromiseConstructorLike): Promise<T | undefined> {
promiseCtor = getPromiseCtor(promiseCtor);

return new promiseCtor((resolve, reject) => {
let value: T | undefined;
this.subscribe(
(x: T) => (value = x),
(err: any) => reject(err),
() => resolve(value)
);
}) as Promise<T | undefined>;
}
}

/**
Expand Down

0 comments on commit 8233648

Please sign in to comment.