Created
November 25, 2017 06:43
-
-
Save devthejo/3bb47bf349a9a2a07ba2a086cc31bf0d to your computer and use it in GitHub Desktop.
This file contains 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 promiseAllRecursive(value) { | |
if (value instanceof Promise) { | |
return value; | |
} | |
if (value instanceof Array) { | |
return Promise.all(value.map(promiseAllRecursive)); | |
} | |
if (typeof value === 'object' && value !== null) { | |
return resolveObject(value); | |
} | |
return value; | |
} | |
function resolveObject(object) { | |
const promises = Object.keys(object).map(key => { | |
const mixed = promiseAllRecursive(object[key]); | |
if(!(mixed instanceof Promise)){ | |
return new Promise((resolve)=>{ | |
resolve({ key, value: mixed }); | |
}); | |
} | |
else{ | |
return mixed.then(value => ({ key, value })); | |
} | |
}); | |
return Promise.all(promises).then(results => { | |
return results.reduce((obj, pair) => { | |
obj[pair.key] = pair.value; | |
return obj; | |
}, {}); | |
}); | |
} | |
export default promiseAllRecursive; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment