亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將異步函數作為參數傳遞

如何將異步函數作為參數傳遞

倚天杖 2022-05-22 10:22:40
我正在使用從 GitHub 存儲庫中提取數據的函數。它返回一個具有諸如已關閉問題數量等指標的對象。此函數作為另一個函數的參數傳遞,該函數將這些指標存儲在數據庫中。store(extract());問題是提取函數是異步的(出于某種原因,它需要是異步的)并且沒有返回值......我不知道如何很好地管理異步。我如何強制 store() 等待 extract() 返回指標?提前致謝。
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

我最終在這里試圖找到類似問題的解決方案,所以對于像我這樣的其他不幸的小伙子,將粘貼我發現的內容,以下工作:


async function A(B: () => Promise<void>) {

    await B();

}

現在我想調用A并傳遞一個異步函數,然后我這樣做:


await A(async () => {

    await wait(3000);

})


查看完整回答
反對 回復 2022-05-22
?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

異步函數只不過是一個函數返回承諾。取樣。


const getPromise = () =>  Promise.resolve("1")


const store = (fn) => {

  fn().then(console.log)

}

store(getPromise)


const storeCB = (fn, cb) => {

  fn().then(cb)

}

store(getPromise, console.log)


const storeThen = (fn) => {

  return fn().then(x => "append: " + x)

}

storeThen(getPromise).then(console.log)


const getAsync = async () =>  "2"


store(getAsync)



const storeWithAwait = async (fn) => {

  const restult = await fn()

  return restult

}


storeWithAwait(getAsync).then(console.log)


查看完整回答
反對 回復 2022-05-22
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

你嘗試過這樣的事情嗎?


(async ()=>{

   const result = await extract();

   store(result);

})()


查看完整回答
反對 回復 2022-05-22
  • 3 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號