Created
August 23, 2017 18:58
-
-
Save derek-watson/5e402073fde51f9a7b49fbbb7aaa37b5 to your computer and use it in GitHub Desktop.
A wrapper for Promise.all that accepts an object of promises and returns an object of response values
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
// extend Promise | |
Promise.allObj = promises => { | |
return Promise | |
.all(Object.values(promises)) | |
.then(values => { | |
return values.reduce((obj, val, i) => { | |
obj[Object.keys(promises)[i]] = val | |
return obj | |
}, {}) | |
}) | |
} | |
// test | |
const one = new Promise(resolve => setTimeout(() => resolve(1), 100)) | |
const two = new Promise(resolve => setTimeout(() => resolve(2), 200)) | |
const three = new Promise(resolve => setTimeout(() => resolve(3), 300)) | |
Promise | |
.allObj({ a: one, b: two, c: three }) | |
.then(values => console.log(values)) // { a: 1, b: 2, c: 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment