javascript - Using Bluebird Promise in For Loop to build and return an Array of Objects -
i trying build , return object using bluebird promises. promise http request gets additional data add object.
i created function carries out request in loop (i using framework carries out middleware - z.
about)
const getwebappcustomfielddetails = (z, url) => { const responsepromise = z.request({ url:url, headers:{ 'content-type': 'application/json' } }); return responsepromise .then(response =>{ return json.parse(response.content); }); };
this function called within following code:
const webappfields = (z, bundle) => { //this section carries creates initial request gets bulk of data const responsepromise = z.request({ url: webappurl(bundle) + '/' + encodeuri(bundle.inputdata.webapp), headers:{ 'content-type': 'application/json' }, }); //this array hold objects created response var fields = []; return responsepromise .then(response => { response = json.parse(response.content); //from response, append core fields response.systemfields.foreach( function (systemfield) { fields.push({ 'key': systemfield.name, 'required': systemfield.required, 'type': systemfield.type.tolowercase() }); }); return response; }) .then(response => { //sometimes there custom fields need retrieved individually const customfieldcount = response.fields.length; var customfieldappend = ''; (var = 0; < customfieldcount; i++){ getwebappcustomfielddetails(z, response.fields[0].links[0].uri) .then(response =>{ customfieldappend = { 'key': response.name, 'required': response.required, 'type': response.type.tolowercase() }; //this push doesn't updated fields array! fields.push(customfieldappend); }); } //this return not include custom fields! return fields; }); };
i cannot figure out how return value nested promise
you can use promise.reduce reduce list of promises creating inside for
loop single promise:
... .then(response => { const customfieldcount = response.fields.length; var customfieldappend = ''; // promice.reduce return accumulator "totalfields" return promise.reduce(response.fields, (totalfields, field) => { getwebappcustomfielddetails(z, field.links[0].uri) // using "field" variable provided reducer function .then(response => { customfieldappend = { 'key': response.name, 'required': response.required, 'type': response.type.tolowercase() }; // add data accumulator "totalfields" totalfields.push(customfieldappend); }); }, []); // third argument initial value of accummulator. in example array, initial value empty array. }); ...
promise.reduce
takes in input 3 arguments: list of arguments loop on (response.fields
), reducer
function , initial value accumulator. accumulator (which called totalfields
) first argument of reducer
function, variable used reduce values in list in single value. in case accumulator array (the fields
array used in example) initial value empty array.
inside reduce
function can access single elements of list (second argument) , can call async operation , fill accumulator ad every step. promise.reduce
function return accumulator wrapped inside promise. reason function can directly returns promise.reduce
's returned promise.
Comments
Post a Comment