Skip to content

Instantly share code, notes, and snippets.

@hrdtbs
Created April 10, 2018 06:19
Show Gist options
  • Save hrdtbs/c8bd73215b133a520cd32ba07e3a6256 to your computer and use it in GitHub Desktop.
Save hrdtbs/c8bd73215b133a520cd32ba07e3a6256 to your computer and use it in GitHub Desktop.
object promise all
function asyncPrint(str, time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`${new Date()}: ${str}`);
resolve(`value ${str}`);
}, time);
});
}
const objectZip = (keys, values) =>
keys.reduce(
(others, key, index) => ({
...others,
[key]: values[index]
}),
{}
);
const objectPromise = async obj =>
objectZip(Object.keys(obj), await Promise.all(Object.values(obj)));
console.log(
objectPromise({
1: asyncPrint("process A", 1000),
2: asyncPrint("process B", 2000)
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment