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

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

我需要等待我的異步函數(shù),然后返回嗎?

我需要等待我的異步函數(shù),然后返回嗎?

POPMUISE 2023-08-05 20:43:56
我有兩個(gè)功能。1.async function firstFunction() {  // Do stuff here  // I can't just return doSomeOtherThings because the firstFunction  // returns different data then what doSomeOtherThings does  doSomeOtherThings();  return;}async function doSomeOtherThings() {  // do promise stuff here  // this function runs some db operations}如果我運(yùn)行,firstFunction();它會(huì)執(zhí)行我的doSomeOtherThings()函數(shù),還是會(huì)提前返回并導(dǎo)致部分或全部 doSomeOtherThings 代碼不被執(zhí)行?我需要做什么嗎await doSomeOtherThings()?
查看完整描述

2 回答

?
料青山看我應(yīng)如是

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

我認(rèn)為這里有點(diǎn)混亂,所以我會(huì)嘗試從頭開(kāi)始。


首先,異步函數(shù)總是返回一個(gè)承諾。如果您在其中添加另一個(gè)異步函數(shù),您可以鏈接它們并在返回第一個(gè)承諾之前等待響應(yīng)。但是,如果您不等待內(nèi)部函數(shù),則第一個(gè)函數(shù)將在第二個(gè)函數(shù)仍在運(yùn)行時(shí)解析。


async function firstFunction() {

  if (I want to wait for doSomeOtherThings to finished before ending firstFunction){

    await   doSomeOtherThings();

  } else if (I can finish firstFUnction and let doSomeOtherTHings finish later){

    doSomeOtherThings

  }


  return;

}


async function init() {

  const apiResponse = await firstFunction();

};


init();


查看完整回答
反對(duì) 回復(fù) 2023-08-05
?
互換的青春

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

你需要等待。實(shí)際上“async/await”只是一個(gè)語(yǔ)法糖。你的 updateOtherThings 函數(shù)實(shí)際上返回一個(gè) Promise,如果你不等待它,那么它就不會(huì)運(yùn)行。如果你只是想開(kāi)始并忘記它,那么寫(xiě)如下:

    updateOtherThings().then(() => {});


查看完整回答
反對(duì) 回復(fù) 2023-08-05
  • 2 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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