Having difficulty wrapping my head around observables. I have the following:
const check = this.http.get(myUrl);
This returns JSON data. I want to test that the "url" field exists in the JSON data. If it doesn't I'd like to query a second URL. (Assume the API is fixed and two calls are necessary, because that is in fact the case.)
The problem is that I can't figure out how to do that. If check
was just JSON (and no an observable) this would be trivial:
if(check.url) {
return check;
} else {
return this.http.get(myOtherUrl);
}
This clearly doesn't work as I can't directly check the URL property. Reading the page on RxJS operators, I've written the following:
const check = this.http.get(myUrl);
const mapResponse = map(response => {
...format data into JSON structure...
return jsonResponse;
});
mapResponse(check).subscribe(result => {
if (result['url']) {
return result;
} else {
return this.http.get(myOtherUrl);
}
});
This looks crazy awkward, as in order to use the second get
I'd have to replicate the code within the lambda function. Not sure how to access the result of the second http
call to actually do something with it. Should I just nest the entire thing again?
Aucun commentaire:
Enregistrer un commentaire