Last active
September 28, 2016 19:27
-
-
Save iSkore/01421f3e48c2b904a39622a0cc4beb46 to your computer and use it in GitHub Desktop.
Recursive Promise Resolution
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
// With Lodash | |
let resolveAll = P => { | |
let map = ( pl, next ) => Promise.all( pl.map( p => Promise.resolve( p ).then( next ) ) ), | |
props = o => { | |
let pToR = []; | |
_.map( _.keys( o ), k => pToR.push( Promise.resolve( o[ k ] ).then( v => _.set( o, k, v ) ) ) ); | |
return Promise.all( pToR ).return( o ); | |
}, | |
rNP = o => Promise.resolve( o ).then( o => { | |
if( _.isArray( o ) ) return map( o, rNP ); | |
else if( _.isPlainObject( o ) ) { | |
let oa = {}; | |
for( let ka in o ) oa[ ka ] = rNP( o[ ka ] ); | |
return props( oa ); | |
} | |
return o; | |
} ); | |
return ( rNP )( P ); | |
}; | |
// Native Code | |
let resolveAll = P => { | |
let map = ( pl, n ) => Promise.all( pl.map( p => Promise.resolve( p ).then( n ) ) ), | |
props = o => { | |
let arr = []; | |
Object.keys( o ).map( k => arr.push( Promise.resolve( o[ k ] ).then( v => ( o[ k ] = v, o ) ) ) ); | |
return Promise.all( arr ).then( () => ( o ) ); | |
}, | |
rNP = o => Promise.resolve( o ).then( o => { | |
if( Array.isArray( o ) ) return map( o, rNP ); | |
else if( typeof o === 'object' ) { | |
let oa = {}; | |
for( let ka in o ) oa[ ka ] = rNP( o[ ka ] ); | |
return props( oa ); | |
} | |
return o; | |
} ); | |
return ( rNP )( P ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment