Skip to content

Instantly share code, notes, and snippets.

@devthejo
Created November 25, 2017 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devthejo/3bb47bf349a9a2a07ba2a086cc31bf0d to your computer and use it in GitHub Desktop.
Save devthejo/3bb47bf349a9a2a07ba2a086cc31bf0d to your computer and use it in GitHub Desktop.
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