Created
April 10, 2018 06:19
-
-
Save hrdtbs/c8bd73215b133a520cd32ba07e3a6256 to your computer and use it in GitHub Desktop.
object promise all
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 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