Created
July 25, 2018 06:08
-
-
Save lbsonley/c7f487574d1c5a2eb079271c00453647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetchSynchronous(fetchFactories) { | |
// we have to fetch the data sequentially in order to avoid overloading the server | |
// this array will hold the data from each response | |
const data = []; | |
// init a promise chain | |
let p = Promise.resolve(); | |
// execute promises in a chain | |
fetchFactories.forEach((fetchFactory) => { | |
// fetchFactories will execute one at a time | |
// pushing each response to an array that will contain all data | |
// when all fetchFactories have been executed | |
p = p.then(fetchFactory) | |
.then((response) => { | |
// place the data into an array | |
data.push(response); | |
// and then return the array to the promise chain | |
return data; | |
}) | |
.catch(err => Promise.reject(err)); | |
}); | |
// return the promise chain which will resolve to an array | |
// containing the sequentially fetched data | |
return p; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment