10-25-2018 20:03
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


10-25-2018 20:03
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I'm trying to call 2 APIs that require location as input (say for example 2 weathers provider). The 2 fetch functions are therefore included into the same response "locationSuccess(position)".
My problem is very simple and I'm sure kinda stupid for developers but I'm very beginner: how do I merge the results returned by the 2 fetch before to send them back to the device?
I have something like
fetch(ENDPOINT_weatherproviderlink1) .then(function (response) { response.json() .then(function(data) { // We just want some data var weather1 = { temperature: data["temp"] } }); }) .catch(function (err) { console.log("Error fetching weather: " + err); }); };
fetch(ENDPOINT_weatherproviderlink2) .then(function (response) { response.json() .then(function(data) { // We just want some data var weather2 = { temperature: data["temp"] } }); }) .catch(function (err) { console.log("Error fetching weather: " + err); }); };
// Send the weather data to the device returnWeatherData(weather);
// merge weather1 + weather2 ??????
Answered! Go to the Best Answer.

Accepted Solutions
11-02-2018 19:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


11-02-2018 19:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
You can use promise.all to perform multiple fetch() commands and wait for the results.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
Some examples here https://stackoverflow.com/questions/31710768/how-can-i-fetch-an-array-of-urls-with-promise-all#31711...

11-02-2018 19:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


11-02-2018 19:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
You can use promise.all to perform multiple fetch() commands and wait for the results.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
Some examples here https://stackoverflow.com/questions/31710768/how-can-i-fetch-an-array-of-urls-with-promise-all#31711...

11-06-2018 18:27 - edited 11-06-2018 18:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


11-06-2018 18:27 - edited 11-06-2018 18:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thanks @JonFitbit. It definitely looks "promising" (see what I did here! )However also very complex, time to put my head in the books!

