Creates a new Observable with this Subject as the source. You can do this to create custom Observer-side logic of the Subject and conceal it from code that uses the Observable.
Creates a new Observable with this Subject as the source. You can do this to create custom Observer-side logic of the Subject and conceal it from code that uses the Observable.
Used as a NON-CANCELLABLE means of subscribing to an observable, for use with APIs that expect promises, like async/await. You cannot unsubscribe from this.
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.
Example
import { interval,take } from 'rxjs';const source$ =interval(1000).pipe(take(4));async function getTotal() { let total = 0; await source$.forEach(value => { total += value; console.log('observable -> ' + value); }); return total;}getTotal().then( total => console.log('Total: ' + total));// Expected:// 'observable -> 0'// 'observable -> 1'// 'observable -> 2'// 'observable -> 3'// 'Total: 6'
Presentation
forEach(next: (value: T) => void): Promise<void>;
Parameters
Name
Type
Description
next
(value: T) => void
a handler for each value emitted by the observable
Returns
Promise<void>
Overload #1
Used as a NON-CANCELLABLE means of subscribing to an observable, for use with APIs that expect promises, like async/await. You cannot unsubscribe from this.
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.
Example
import { interval,take } from 'rxjs';const source$ =interval(1000).pipe(take(4));async function getTotal() { let total = 0; await source$.forEach(value => { total += value; console.log('observable -> ' + value); }); return total;}getTotal().then( total => console.log('Total: ' + total));// Expected:// 'observable -> 0'// 'observable -> 1'// 'observable -> 2'// 'observable -> 3'// 'Total: 6'
Presentation
forEach(next: (value: T) => void): Promise<void>;
Parameters
Name
Type
Description
next
(value: T) => void
a handler for each value emitted by the observable