I have been struggling with this for quite sometime and haven't been able to find a good solution for this problem.
I have an array which I map through an imported function that has a call back. I want to get the data(JSON) from the callback, manipulate it, output it and run it through another function.
Because the node_module qbo.getBill function has a callback, I don't think I can run promise or async\\await, well I haven't had any success doing so as soon as I add the getBill function.
Here is my code/what I am trying to do:
var QuickBooks = require('node-quickbooks') var qbo = new QuickBooks(...) let BillIds = [....] let newArray =[] let mapData = BillIds.map(val => { qbo.getBill(val, function (err, Billid) { newArray.push = { id: Billid.Id, balance: Billid.Balance } }) }) DoAThingWithAsyncronousData(newArray)
For the life of me I can't get any action to wait on the the asynchronous data before completing the next step, where am I going wrong?
Sure you can use Promise.all, you just need to put a promise around getBill
's callback:
const newArray = await Promise.all(
BillIds.map(val => new Promise((resolve, reject) => {
qbo.getBill(val, (err, Billid) => {
const billObj = {
id: Billid.Id,
balance: Billid.Balance
}
resolve(billObj)
});
}))
);
DoAThingWithAsyncronousData(newArray)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.