Skip to content

Instantly share code, notes, and snippets.

@bkawk
Last active October 15, 2017 09:17
Show Gist options
  • Save bkawk/4e024612c012166d4b30878989f6b16c to your computer and use it in GitHub Desktop.
Save bkawk/4e024612c012166d4b30878989f6b16c to your computer and use it in GitHub Desktop.
Reduce with promise
function _test(actions){
return actions.reduce((chain, action) => {
return chain.then(() => action.functionToCall(action.argumentToSend)).then(val => console.log(val));
}, Promise.resolve());
}
function _one(data){
return new Promise((resolve, reject) => {
setTimeout(function(){
console.log(data);
resolve('resolvedFromOne');
}, 200);
})
}
function _two(data){
return new Promise((resolve, reject) => {
setTimeout(function(){
console.log(data);
resolve('resolvedFromTwo');
}, 200);
})
}
function _three(data){
return new Promise((resolve, reject) => {
setTimeout(function(){
console.log(data);
resolve('resolvedFromThree');
}, 200);
})
}
const arrayOfObjects = [
{ functionToCall: _one, argumentToSend: 'Yay function one was called with this argument' },
{ functionToCall: _two, argumentToSend: 'Yay function two was called with this argument' },
{ functionToCall: _three, argumentToSend: 'Yay function three was called with this argument' },
];
_test(arrayOfObjects);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment