Decides at subscription time which Observable will actually be subscribed.
If
statement for Observables.
iif
accepts a condition function and two Observables. When
an Observable returned by the operator is subscribed, condition function will be called.
Based on what boolean it returns at that moment, consumer will subscribe either to
the first Observable (if condition was true) or to the second (if condition was false). Condition
function may also not return anything - in that case condition will be evaluated as false and
second Observable will be subscribed.
Note that Observables for both cases (true and false) are optional. If condition points to an Observable that was left undefined, resulting stream will simply complete immediately. That allows you to, rather than controlling which Observable will be subscribed, decide at runtime if consumer should have access to given Observable or not.
If you have more complex logic that requires decision between more than two Observables, {@link defer}
will probably be a better choice. Actually iif
can be easily implemented with {@link defer}
and exists only for convenience and readability reasons.
import { iif, of } from 'rxjs';
let subscribeToFirst;
const firstOrSecond = iif(
() => subscribeToFirst,
of('first'),
of('second'),
);
subscribeToFirst = true;
firstOrSecond.subscribe(value => console.log(value));
// Logs:
// "first"
subscribeToFirst = false;
firstOrSecond.subscribe(value => console.log(value));
// Logs:
// "second"
let accessGranted;
const observableIfYouHaveAccess = iif(
() => accessGranted,
of('It seems you have an access...'), // Note that only one Observable is passed to the operator.
);
accessGranted = true;
observableIfYouHaveAccess.subscribe(
value => console.log(value),
err => {},
() => console.log('The end'),
);
// Logs:
// "It seems you have an access..."
// "The end"
accessGranted = false;
observableIfYouHaveAccess.subscribe(
value => console.log(value),
err => {},
() => console.log('The end'),
);
// Logs:
// "The end"
Condition which Observable should be chosen.
Either first or second Observable, depending on condition.
Creates an Observable that emits no items to the Observer and immediately emits an error notification.
Just emits 'error', and nothing else.
This static operator is useful for creating a simple Observable that only emits the error notification. It can be used for composing with other Observables, such as in a {@link mergeMap}.
import { throwError, concat, of } from 'rxjs';
const result = concat(of(7), throwError(new Error('oops!')));
result.subscribe(x => console.log(x), e => console.error(e));
// Logs:
// 7
// Error: oops!
import { throwError, interval, of } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
interval(1000).pipe(
mergeMap(x => x === 2
? throwError('Twos are bad')
: of('a', 'b', 'c')
),
).subscribe(x => console.log(x), e => console.error(e));
// Logs:
// a
// b
// c
// a
// b
// c
// Twos are bad
The particular Error to pass to the error notification.
An error Observable: emits only the error notification using the given error argument.
Creates a new Observable with this Subject as the source. You can do this to create customize Observer-side logic of the Subject and conceal it from code that uses the Observable.
Observable that the Subject casts to
a handler for each value emitted by the observable
a promise that either resolves on observable completion or rejects with the handled error
Emits the resolution
and completes.
Generated using TypeDoc
Internal implementation detail, do not use directly.