Based on the parameters of the function I want to build the forkJoin() method.
For example:
Code:
getAllByIds(parameter1: any, parameter2: any) {
let itemList = new Array();
return Observable.forkJoin(
this.http.get('rest/fillin/ids/' + parameter1) // don't put this request for parameter1 if it is empty
.map((res: Response) => res.json()),
this.http.get('rest/textitem/ids/' + parameter2) // don't put this request for parameter2 if it is empty
.map((res:Response) => res.json())
).map(
data => {
itemList.push(data[0]);
itemList.push(data[1]);
return itemList;
}
);
}
So, is it possible to build up the forkJoin() like this?
forkJoin is an operator that takes any number of input observables which can be passed either as an array or a dictionary of input observables. If no input observables are provided (e.g. an empty array is passed), then the resulting stream will complete immediately.
The forkJoin observable now takes a dictionary of sources: Moreover, there is one deprecation — forkJoin(a, b, c, d) should no longer be used; Instead, pass an array such as forkJoin([a, b, c, d]) .
“If any input observable errors at some point, forkJoin will error as well and all other observables will be immediately unsubscribed.”
forkJoin takes a number of input observables and waits for all passed observables to complete. Once they are complete, it will then emit a group of the last values from corresponding observables. The resulting stream emits only one time when all of the inner streams complete.
Actually this depends on what do you expect to get when you skip some HTTP requests.
Should the output from forkJoin()
contain null
values or just ignore it completely?
function mockHTTPRequest(id) {
return Observable.of(id).delay(100);
}
let parameter1 = 'a';
let parameter2 = false;
let sources = [];
if (parameter1) {
sources.push(mockHTTPRequest('rest/fillin/ids/' + parameter1));
}
if (parameter2) {
sources.push(mockHTTPRequest('rest/textitem/ids/' + parameter2));
}
Observable.forkJoin(...sources)
.map(data => {
console.log(data.length);
return data;
})
.subscribe(values => console.log(values));
See live demo: https://jsbin.com/qorulel/5/edit?js,console
This solution just doesn't create source Observables if parameter1
or parameter2
is false. Notice, that console.log(data.length)
can be from 0
to 2
depending on parameterX
values.
Or you can just create Observable.of(null)
instead of the HTTP requests.
function mockHTTPRequest(id) {
return Observable.of(id).delay(100);
}
let parameter1 = 'a';
let parameter2 = false;
let sources = [
parameter1 ? mockHTTPRequest('rest/fillin/ids/' + parameter1) : Observable.of(null),
parameter2 ? mockHTTPRequest('rest/textitem/ids/' + parameter2) : Observable.of(null)
];
Observable.forkJoin(...sources)
.map(data => {
console.log(data.length);
return data;
})
.subscribe(values => console.log(values));
See live demo: https://jsbin.com/caheno/5/edit?js,console
Now the output has always 2 values. Just some of them are null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With