Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Adds the "eval" function to Promise (see for details)
Promise.eval = function(ar)
// We accept either an array or items specified as individual arguments...
// ...but we convert the series into an array either way
ar =
var items = { } // map names to items
// This forEach is where it all happens - we evaluate each item within the eval array.
// If there are dependencies, those are first resolved.
// Its the mother of all promise workflows is just 10 lines!
ar.forEach(function(zex, i) {
items[ || ("_p" + i)] =
Promise.all((zex.deps || []).map(function(arg) {
// even if items[arg] is undefined, using "arg in items" is true when the property exists
if(typeof arg === "string" && arg in items)
return items[arg]
return arg
.then(function(dResults) {
var value = zex.value
if(typeof value === "function")
value = value.apply(null, dResults)
return value
// Now we just wait for each promise to complete - then assign the resolved value to its named property
var names = Object.keys(items),
itemVals = { return items[name] }) // Object.values()
return Promise.all(itemVals)
.then(function(paResults) {
var results = { }
names.forEach(function(name, i) {
results[name] = paResults[i]
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment