-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Strawman Proposal for toPromise replacement #5295
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { firstValueFrom } from 'rxjs'; | ||
import { a$ } from 'helpers'; | ||
|
||
describe('firstValueFrom', () => { | ||
const r0 = firstValueFrom(a$); // $ExpectType Promise<A> | ||
const r1 = firstValueFrom(); // $ExpectError | ||
const r2 = firstValueFrom(Promise.resolve(42)); // $ExpectError | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { lastValueFrom } from 'rxjs'; | ||
import { a$ } from 'helpers'; | ||
|
||
describe('lastValueFrom', () => { | ||
const r0 = lastValueFrom(a$); // $ExpectType Promise<A> | ||
const r1 = lastValueFrom(); // $ExpectError | ||
const r2 = lastValueFrom(Promise.resolve(42)); // $ExpectError | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { interval, firstValueFrom, EMPTY, EmptyError, throwError, of } from 'rxjs'; | ||
import { expect } from 'chai'; | ||
import { finalize } from 'rxjs/operators'; | ||
|
||
describe('firstValueFrom', () => { | ||
it('should emit the first value as a promise', async () => { | ||
let finalized = false; | ||
const source = interval(10).pipe(finalize(() => (finalized = true))); | ||
const result = await firstValueFrom(source); | ||
expect(result).to.equal(0); | ||
expect(finalized).to.be.true; | ||
}); | ||
|
||
it('should error for empty observables', async () => { | ||
const source = EMPTY; | ||
let error: any = null; | ||
try { | ||
await firstValueFrom(source); | ||
} catch (err) { | ||
error = err; | ||
} | ||
expect(error).to.be.an.instanceOf(EmptyError); | ||
}); | ||
|
||
it('should error for errored observables', async () => { | ||
const source = throwError(new Error('blorp!')); | ||
let error: any = null; | ||
try { | ||
await firstValueFrom(source); | ||
} catch (err) { | ||
error = err; | ||
} | ||
expect(error).to.be.an.instanceOf(Error); | ||
expect(error.message).to.equal('blorp!'); | ||
}); | ||
|
||
it('should work with a synchronous observable', async () => { | ||
let finalized = false; | ||
const source = of('apples', 'bananas').pipe(finalize(() => (finalized = true))); | ||
const result = await firstValueFrom(source); | ||
expect(result).to.equal('apples'); | ||
expect(finalized).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { interval, lastValueFrom, EMPTY, EmptyError, throwError, of } from 'rxjs'; | ||
import { expect } from 'chai'; | ||
import { finalize, take } from 'rxjs/operators'; | ||
|
||
describe('lastValueFrom', () => { | ||
it('should emit the last value as a promise', async () => { | ||
let finalized = false; | ||
const source = interval(2).pipe( | ||
take(10), | ||
finalize(() => (finalized = true)) | ||
); | ||
const result = await lastValueFrom(source); | ||
expect(result).to.equal(9); | ||
expect(finalized).to.be.true; | ||
}); | ||
|
||
it('should error for empty observables', async () => { | ||
const source = EMPTY; | ||
let error: any = null; | ||
try { | ||
await lastValueFrom(source); | ||
} catch (err) { | ||
error = err; | ||
} | ||
expect(error).to.be.an.instanceOf(EmptyError); | ||
}); | ||
|
||
it('should error for errored observables', async () => { | ||
const source = throwError(new Error('blorp!')); | ||
let error: any = null; | ||
try { | ||
await lastValueFrom(source); | ||
} catch (err) { | ||
error = err; | ||
} | ||
expect(error).to.be.an.instanceOf(Error); | ||
expect(error.message).to.equal('blorp!'); | ||
}); | ||
|
||
it('should work with a synchronous observable', async () => { | ||
let finalized = false; | ||
const source = of('apples', 'bananas').pipe(finalize(() => (finalized = true))); | ||
const result = await lastValueFrom(source); | ||
expect(result).to.equal('bananas'); | ||
expect(finalized).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Observable } from './Observable'; | ||
import { EmptyError } from './util/EmptyError'; | ||
import { Subscription } from './Subscription'; | ||
|
||
/** | ||
* Converts an observable to a promise by subscribing to the observable, | ||
* and returning a promise that will resolve as soon as the first value | ||
* arrives from the observable. The subscription will then be closed. | ||
* | ||
* If the observable stream completes before any values were emitted, the | ||
* returned promise will reject with {@link EmptyError}. | ||
* | ||
* If the observable stream emits an error, the returned promise will reject | ||
* with that error. | ||
* | ||
* ### Example | ||
* | ||
* Wait for the first value from a stream and emit it from a promise in | ||
* an async function. | ||
* | ||
* ```ts | ||
* import { interval, firstValueFrom } from 'rxjs'; | ||
* | ||
* async function execute() { | ||
* const source$ = interval(2000); | ||
* const firstNumber = await firstValueFrom(source); | ||
* console.log(`The first number is ${firstNumber}`); | ||
* } | ||
* | ||
* execute(); | ||
* | ||
* // Expected output: | ||
* // "The first number is 0" | ||
* ``` | ||
* | ||
* @param source the observable to convert to a promise | ||
*/ | ||
export function firstValueFrom<T>(source$: Observable<T>) { | ||
return new Promise<T>((resolve, reject) => { | ||
const subs = new Subscription(); | ||
subs.add( | ||
source$.subscribe({ | ||
next: value => { | ||
resolve(value); | ||
subs.unsubscribe(); | ||
}, | ||
error: reject, | ||
complete: () => { | ||
reject(new EmptyError()); | ||
}, | ||
}) | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Observable } from './Observable'; | ||
import { EmptyError } from './util/EmptyError'; | ||
|
||
/** | ||
* Converts an observable to a promise by subscribing to the observable, | ||
* waiting for it to complete, and resolving the returned promise with the | ||
* last value from the observed stream. | ||
* | ||
* If the observable stream completes before any values were emitted, the | ||
* returned promise will reject with {@link EmptyError}. | ||
* | ||
* If the observable stream emits an error, the returned promise will reject | ||
* with that error. | ||
* | ||
* ### Example | ||
* | ||
* Wait for the last value from a stream and emit it from a promise in | ||
* an async function. | ||
* | ||
* ```ts | ||
* import { interval, lastValueFrom } from 'rxjs'; | ||
* import { take } from 'rxjs/operators'; | ||
* | ||
* async function execute() { | ||
* const source$ = interval(2000).pipe(take(10)); | ||
* const finalNumber = await lastValueFrom(source); | ||
* console.log(`The final number is ${finalNumber}`); | ||
* } | ||
* | ||
* execute(); | ||
* | ||
* // Expected output: | ||
* // "The final number is 9" | ||
* ``` | ||
* | ||
* @param source the observable to convert to a promise | ||
*/ | ||
export function lastValueFrom<T>(source: Observable<T>) { | ||
return new Promise<T>((resolve, reject) => { | ||
let _hasValue = false; | ||
let _value: T; | ||
source.subscribe({ | ||
next: value => { | ||
_value = value; | ||
_hasValue = true; | ||
}, | ||
error: reject, | ||
complete: () => { | ||
if (_hasValue) { | ||
resolve(_value); | ||
} else { | ||
reject(new EmptyError()); | ||
} | ||
}, | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this will handle the degenerate case of an infinite synchronous source. To handle that, I think this would have to unsubscribe via the next callback's
this
instead. Thoughts?