Last active
February 26, 2016 01:20
-
-
Save L3V147H4N/cff5f58cb8bd8264b92c to your computer and use it in GitHub Desktop.
Promise Sequence with things to run
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 sequenceByArgs (runner, thingsToRun = [], fnName, values = [], method = 'apply') { | |
return new Promise((resolve, reject) => { | |
if (thingsToRun.length !== 0) { | |
var thing = (method === 'apply' && !Array.isArray(thingsToRun[0])) ? new Array(thingsToRun[0]) : thingsToRun[0]; | |
runner[fnName][method](runner, thing) | |
.then(data => { | |
values.push(data); | |
thingsToRun.splice(0, 1); | |
sequenceByArgs(runner, thingsToRun, fnName, values) | |
.then(() => { | |
resolve(values); | |
}) | |
.catch(err => { | |
reject(err); | |
}); | |
}) | |
.catch(err => { | |
reject(err); | |
}); | |
} else { | |
resolve(values); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment