[英]Problem with promises / async / await function not waiting for response
我遇到了一個代碼塊的問題,它應該按順序運行一系列異步函數。 似乎一個 await 不能正常工作。 在此關閉之前,我查看了我可以在 stackOverflow 上找到的每個相關線程,但我似乎無法解決此問題或理解為什么我的代碼沒有像應有的那樣等待。
我有一個 function,它將每分鍾在服務器上運行一次,因此它需要異步,以免干擾 API 的 rest。
代碼如下。 如果有人可以解釋為什么會發生此錯誤,將不勝感激。
初次通話
setInterval(() => sn.checkQuoteRequest(), 5000) //10 seconds for testing but will be 1 min in deployment
主要 function 問題出在哪里
exports.checkQuoteRequest = async () =>
{
try {
return new Promise(async function (resolve, reject) {
let inquries = new Array;
let quoted = new Array;
await loadCustomerInq()
.then(async (response) => {
console.log("Load Customer INQ Response:", response);
inquries = await response;
await filterExpiredInq(inquries)
.then(async(response_1) => {
quoted = await response_1;
console.log("Quoted:", quoted);
await fetchCustomerSubscriptions(quoted)//should complete before next function
.then(async (response_3) => {
let subscriptions = await response_3;
await sendCustomerOfferNotification(subscriptions) //runs before
//prior function finished
.then(async() =>
{
for(q of quoted) {
await setQuoteNotified(q)
.then(async(response) => {
console.log(q, "Notified", await response)
})
.catch((err) => {
console.log("Set Quoted Notified ERR!", err);
});
};
resolve();
})
.catch((err) => console.log(err));
});
});
});
});
}
catch (err_1) {
console.log("Load Customer Inq ERR!", err_1);
}
}
應該在 sendOfferNotification 之前執行的 function
fetchCustomerSubscriptions = (quoted) =>
{
let subs = new Array;
return new Promise(function(resolve, reject)
{
for(q of quoted)
{
console.log("Fetch Cus Subs inqID: ", q.inqid)
pool.query(`SELECT endpoint, expirationTime, p256dh, auth FROM userSubscriptionsView WHERE (inqID = $1 AND notified = $2)`, [q.inqid, false],
(err,result) =>
{
if(err)
console.log(err)
else
{
console.log("Customer Subscriptions", result.rows)
result.rows.forEach((r) =>
{
console.log("Adding new Customer offer Sub")
subs.push(new subscription(r))
})
console.log("New Subs", subs)
}
})
}
resolve(subs)
})
}
output
Load Customer INQ Response: [
{ inqid: 20, expiry: 2020-07-01T00:00:00.000Z },
{ inqid: 21, expiry: 2020-07-01T00:00:00.000Z }
]
Quoted: [
{ inqid: 20, expiry: 2020-07-01T00:00:00.000Z },
{ inqid: 21, expiry: 2020-07-01T00:00:00.000Z }
]
Fetch Cus Subs inqID: 20
Fetch Cus Subs inqID: 21
0
{ message: 'Notification sent successfully.' }
Set Quote Notified inq { inqid: 20, expiry: 2020-07-01T00:00:00.000Z }
Customer Subscriptions []
New Subs []
{ inqid: 20, expiry: 2020-07-01T00:00:00.000Z } Notified true
Set Quote Notified inq { inqid: 21, expiry: 2020-07-01T00:00:00.000Z }
Customer Subscriptions [
{
endpoint: 'https://fcm.googleapis.com/fcm/send/dgYoxNCLMKg:APA91bGyrs5nMIcd3ICta_B_D0tzlkQ34R63TRfHsmBMlUjDCq1wkyprbjNmhUa20MItwQwYIqovZI4xMF0V5VA5Ns0QRqxlRtX_aEhxTo5wfEsuvZoFZaXHihGWaCXFmyCE0qJZnXpT',
expirationtime: null,
p256dh: 'BLHt6QPUE2Co3ad9-5wG9f2c46_fph9fxud2zNHRORogttjfg7aS6xMxZYWYQE3DOZAsnJoxivBuhxa7_FrRftc',
auth: 'Xb9v3Tsuhd2T0RDNXn6LxA'
},
{
endpoint: 'https://fcm.googleapis.com/fcm/send/fhGYWDttRJA:APA91bGeKElvyp2In2ZufGEMLVwM6yz6qPaIBj2h5Hw2tYrRjI2KuPMBYAkSyJTeoRXViHUrvTM_ryUBYqnuCaPTtXk8HeHWBXZHAuic2v-fjrVZT6AOKafwGAutnlHW1C5L2lAuQbyj',
expirationtime: null,
p256dh: 'BAfCoSCqSO6-_zDmKXfherzDUhWw_QWHuRtdwU3LK8Yx5LmnndqwuxaTJEI4M14pjx0AX-BmOo2Md0JLevcNG4M',
auth: '2ZHkYLkvLcVqaK26fU7E7g'
}
]
Adding new Customer offer Sub
Adding new Customer offer Sub
New Subs [
subscription {
endpoint: 'https://fcm.googleapis.com/fcm/send/dgYoxNCLMKg:APA91bGyrs5nMIcd3ICta_B_D0tzlkQ34R63TRfHsmBMlUjDCq1wkyprbjNmhUa20MItwQwYIqovZI4xMF0V5VA5Ns0QRqxlRtX_aEhxTo5wfEsuvZoFZaXHihGWaCXFmyCE0qJZnXpT',
expirationTime: undefined,
keys: {
p256dh: 'BLHt6QPUE2Co3ad9-5wG9f2c46_fph9fxud2zNHRORogttjfg7aS6xMxZYWYQE3DOZAsnJoxivBuhxa7_FrRftc',
auth: 'Xb9v3Tsuhd2T0RDNXn6LxA'
}
},
subscription {
endpoint: 'https://fcm.googleapis.com/fcm/send/fhGYWDttRJA:APA91bGeKElvyp2In2ZufGEMLVwM6yz6qPaIBj2h5Hw2tYrRjI2KuPMBYAkSyJTeoRXViHUrvTM_ryUBYqnuCaPTtXk8HeHWBXZHAuic2v-fjrVZT6AOKafwGAutnlHW1C5L2lAuQbyj',
expirationTime: undefined,
keys: {
p256dh: 'BAfCoSCqSO6-_zDmKXfherzDUhWw_QWHuRtdwU3LK8Yx5LmnndqwuxaTJEI4M14pjx0AX-BmOo2Md0JLevcNG4M',
auth: '2ZHkYLkvLcVqaK26fU7E7g'
}
}
]
{ inqid: 21, expiry: 2020-07-01T00:00:00.000Z } Notified true
這兩行應該在最后
0
{ message: 'Notification sent successfully.' }
立即調用resolve()
function,無需等待pool.query()
完成。 您需要將每次調用pool.query(
) 的結果視為單獨的承諾。
fetchCustomerSubscriptions = (quoted) =>
{
const promises = []
for(q of quoted)
{
console.log("Fetch Cus Subs inqID: ", q.inqid)
const promise = new Promise((resolve, reject) => {
pool.query(`SELECT endpoint, expirationTime, p256dh, auth FROM userSubscriptionsView WHERE (inqID = $1 AND notified = $2)`, [q.inqid, false],
(err,result) =>
{
if(err)
console.log(err)
else
{
const subs = []
console.log("Customer Subscriptions", result.rows)
result.rows.forEach((r) =>
{
console.log("Adding new Customer offer Sub")
subs.push(new subscription(r))
})
console.log("New Subs", subs)
resolve(subs)
}
})
})
promises.push(promise)
}
return Promise.all(promises)
.then(subs_list => {
return [].concat.apply([], subs_list) // flaten [[sub1, sub2], [sub3]] -> [sub1, sub2, sub3]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.