Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsphkhan/2340126012376811ff6c058eb10ca162 to your computer and use it in GitHub Desktop.
Save jsphkhan/2340126012376811ff6c058eb10ca162 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