10-25-2018 20:03
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
10-25-2018 20:03
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.
Best Answer11-02-2018 19:06
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
11-02-2018 19:06
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...
Best Answer11-02-2018 19:06
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
11-02-2018 19:06
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...
Best Answer11-06-2018 18:27 - edited 11-06-2018 18:28
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
11-06-2018 18:27 - edited 11-06-2018 18:28
Thanks @JonFitbit. It definitely looks "promising" (see what I did here!
)However also very complex, time to put my head in the books!
Best Answer