第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Await 關(guān)鍵字在承諾履行之前沒(méi)有正確停止?

Await 關(guān)鍵字在承諾履行之前沒(méi)有正確停止?

牛魔王的故事 2024-01-18 15:43:59
所以我試圖在異步函數(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;    };
查看完整描述

1 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

就你而言,data['option_activity'].map這不是一個(gè)承諾。


看這個(gè)例子:


let sample_arr= Array.from({ length: 10}, () => Math.floor(Math.random() * (10 - 1) + 1));


let f= async(arr)=>{

  await Promise.all(arr.map(async item=>console.log(

    await new Promise(r=>setTimeout(()=>r(item),2000)))

  ));

  console.log('after all promises concurrent fulfilled.');

}


f(sample_arr);


查看完整回答
反對(duì) 回復(fù) 2024-01-18
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)