所以我試圖在異步函數(shù)中操作這個(gè)變量,并且我知道該變量是在我將在下面顯示的 data.map 函數(shù)完成之前返回的,但我很好奇為什么?我在前面有 wait 關(guān)鍵字,試圖暫停代碼,直到 .map 函數(shù)完成,然后它應(yīng)該從 .map 函數(shù)返回計(jì)數(shù),但事實(shí)并非如此,我很好奇我做錯(cuò)了什么以及是否有有更好的方法嗎?謝謝你!代碼:const alertFetch = async () => { //set a count at the beginning of the endpoint var counter = 0; await data["option_activity"].map(async (item) => { var reply = await GET_ASYNC("alert_" + item.id); if (reply) { //If they are a repeat return nothing // console.log('No New Alert Detected') } else { //If they arent a repeat store the alert // console.log("New alert detected") const saveResult = await SET_ASYNC( "alert_" + item.id, JSON.stringify(item), "EX", 604800 ); //Push it to array list in redis await client.get("alert_" + item.id, function (err, reply) { client.rpush("alerts", reply); }); //Increment counter to represent how many new alers counter = counter + 1; } }); console.log(counter); return counter; };
Await 關(guān)鍵字在承諾履行之前沒(méi)有正確停止?
牛魔王的故事
2024-01-18 15:43:59