I use promise to handle async method getPropoalByPeriod
but I can't get the obj
data out of the foo
function where console.log(89,foo(mycallback()))
prints undefined. Please help to get data outside so that I can process them.
function foo(callback) {
var MyPromises = periods.map((p) => {
return new Promise((resolve, reject) => {
myFunctions.getPropoalByPeriod(p.id, localStart, function(err, rows) {
if (err) {
console.log(62, err);
return reject(err);
} else
var obj = {
id: p.id,
name: p.name,
savedServices: rows[0] ? rows[0].services : '',
};
resolve(obj)
})
})
})
Promise.all(MyPromises)
.then((p) => {
callback(p)
})
.catch((err) => console.log(79, err));
}
function mycallback(res) {
console.log(85, res)
return res
}
foo(mycallback)
console.log(89, foo(mycallback()))
This issue is different from what is on SO. It is using map() where I couldn't figure out how to apply await compared to the straightforward example of promise or async/await.