Skip to content

Instantly share code, notes, and snippets.

@L3V147H4N
Last active February 26, 2016 01:20
Show Gist options
  • Save L3V147H4N/cff5f58cb8bd8264b92c to your computer and use it in GitHub Desktop.
Save L3V147H4N/cff5f58cb8bd8264b92c to your computer and use it in GitHub Desktop.
Promise Sequence with things to run
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