Last active
October 15, 2017 09:17
-
-
Save bkawk/4e024612c012166d4b30878989f6b16c to your computer and use it in GitHub Desktop.
Reduce with promise
This file contains hidden or 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
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