Skip to content

Instantly share code, notes, and snippets.

@lbsonley
Created July 25, 2018 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lbsonley/c7f487574d1c5a2eb079271c00453647 to your computer and use it in GitHub Desktop.
Save lbsonley/c7f487574d1c5a2eb079271c00453647 to your computer and use it in GitHub Desktop.
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