Created
August 15, 2017 05:52
-
-
Save AntJanus/01e4e1eb4d0ec87723affdec4b9f6217 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
// 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