4 Jan, 2017
function runInParallel(input) {
let keys = [];
let promises = [];
for (let key in input) {
keys.push(key);
promises.push(input[key]);
}
return Promise.all(promises).then((resolved) => {
let output = {};
for (let i=0; i<resolved.length; i++) {
output[keys[i]] = resolved[i];
}
return output;
});
}
const p1 = Promise.resolve(3);
const p2 = 1337;
const p3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, "foo");
});
function p4() {
return Promise.resolve(4);
}
runInParallel({p1, p2, p3, p4}).then(values => {
console.log(values); // { p1: 3, p2: 1337, p3: 'foo', p4: [Function: p4] }})
});
Copyright James Gardner 1996-2020 All Rights Reserved. Admin.