javascript - Promise.map function .then() block getting null before nested promise resolves -


i'm kicking off nested promise mapping , seeing outer .then() block print null result before resolve in function called.

i feel must messing syntax somehow. i've made stripped down example:

const promise = require('bluebird'); const toparray = [{outerval1: 1,innerarray: [{innerval1: 1,innerval2: 2}, {innerval1: 3,innerval2: 4}]},{outerval2: 2,innerarray: [{innerval1: 5, innerval2: 6 },  {innerval1: 7,innerval2: 8 }]}] ;  promisewithoutdelay = function (innerobject) {     return new promise(function (resolve, reject) {         settimeout(function () {             console.log("promisewithdelay" ,innerobject);             let returnval = {}             returnval.innerval1 = innerobject.innerval1;             returnval.innerval2 = innerobject.innerval2;             returnval.delay = false;              return resolve(returnval);         }, 0);     }) } promisewithdelay = function (innerobject) {     return new promise(function (resolve, reject) {         settimeout(function () {             console.log("promisewithdelay" ,innerobject);             let returnval = {}              returnval.innerval1 = innerobject.innerval1;             returnval.innerval2 = innerobject.innerval2;             returnval.delay = true;             return resolve(returnval);         }, 3000);     }) }  test1 = function () {     let newarray = [];     let newarrayindex = 0;      promise.map(toparray, function (outerobject) {          promise.map(outerobject.innerarray, function (innerobject) {              promise.all([                 promisewithoutdelay(innerobject),                 promisewithdelay(innerobject)             ])                 .then(function (promiseresults) {                     newarray[newarrayindex++] = {result1: promiseresults[1], result2: promiseresults[2]}                  })         })     })         .then(function () {             return newarray;         }) }  var result = test1();  console.log("got result ",result); 

what i'm trying loop on outer array has values need.
these values include nested inner array must loop on values.
in inner loop pass outer , inner values promise functions in promise.all.
when promise functions resolve assigned return object.
seems working fine except 1 of promise functions has delay it's doing calculations.
when happens left out of return value because hasn't resolved yet.

shouldn't wait until inner loop promise.all resolves before returns outer loop?

can point me in right direction?

edit: ended solution based on @thomas's suggestion:

test1 = function(){     return promise.map(toparray, function(outerobject){         let oval = outerobject.outerval;         return promise.map(outerobject.innerarray, function(innerobject){             innerobject.oval = oval;             return promise.all([ promisewithdelay(innerobject), promisewithoutdelay(innerobject)])                 .then(function(results) {                     return { result1: results[0], result2: results[1], delay: results[2] } ;                 })         })     }).reduce(function(newarray, arr){         return newarray.concat(arr);     }, []); } 

i'm not entirely sure problem stripped down example, think want here this:

test1 = function(){     return promise.map(toparray, function(outerobject){         return promise.all(outerobject.innerarray)     }).reduce(function(newarray, arr){         return newarray.concat(arr);     }, []); } 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -