Skip to content

Instantly share code, notes, and snippets.

@AntJanus
Created August 15, 2017 05:52
Show Gist options
  • Save AntJanus/01e4e1eb4d0ec87723affdec4b9f6217 to your computer and use it in GitHub Desktop.
Save AntJanus/01e4e1eb4d0ec87723affdec4b9f6217 to your computer and use it in GitHub Desktop.
// weird method 1
function someAction() {
return fetchData()
.then(data => {
return cumulativePromise([data], fetchOtherData());
})
.then(accumulation => {
var fetchedData = accumulation[0];
var fetchedOtherData = accumulation[1];
return cumulativePromise(accumulation, saveNewData(fetchedData));
})
.then((accumulation => {
// ..etc..etc
});
}
function cumulativePromise(values, newPromise) {
values = values.map((value) => {
return new Promise((resolve, reject) => {
return resolve(value);
});
values.push(newPromise);
return Promise.all(values);
}
// method 2
function someAction() {
// promises have to be aware of how to deal with an accumulation
var promises = [
fetchData,
fetchOtherData,
saveData
];
return Promise.reduce(promises, (accumulation, promise) => {
return promise(accumulation)
.then((val) => {
accumulation.push(val);
return accumulation;
})
;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment