node.js - Named promise results with q.all in NodeJS -
i'm kinda new q
stuff , find pretty awesome, there's still can't figure out.
i managed run combined promises q.all
passing q.all
array of promises. this..
var promises = [promiseone(), promisetwo()]; q.all(promises).then(function (results) { res.send(results); } );
the thing want promises named, don't have rely in order of promises.
i read somewhere can pass object q.all
, have results named. this:
var promises = { promiseone: promiseone(), promisetwo: promisetwo() } q.all(promises).then(function(results){ res.send(results); });
but guess doesn't work same way sending array i'm not getting results of promises in there. result similar one:
{ promiseone: { source: {} }, promisetwo: { source: {} } }
so how go getting named results q.all?
one thing note amount of promises have in promises
array not fixed get
param sent user function.
also, inside each of promises have array (or object) of promises resolved , results named well.
here way write code roamer wrote functionality asked (return object):
q.props = obj => { const ps = q.all(object.keys(obj).map(x => q.all([x, obj[x]]))); return ps.then(x => x.reduce((p, c) => {p[c[0]] = c[1]; return p } , {})); };
which let do:
q.props(promises).then(o => { o.promiseone });
although should consider using bluebird if want these helper functions.
Comments
Post a Comment