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

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

使函數調用同步

使函數調用同步

慕桂英4014372 2022-12-22 15:49:56
function first(){    console.log("1")}function second(){     new Promise ((resolve,reject)=>{        setTimeout(function(){            console.log("2")            resolve();        } ,0);     })  }function third(){    console.log("3")} async function run(){       first();    await second();    third();}run();需要使函數調用同步以獲得最終輸出 1,2,3 我嘗試創建 promise 并使用 async await 但這對任何其他方式都沒有幫助
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

將 setTimeout 打包成一個 promise 并在 setTimeout 中解析,


對該承諾使用 async await 以使其連續運行


function first() {

  console.log("1")

}


function second() {

  return new Promise(res => {

    setTimeout(function() {

      console.log("2");

      res()

    }, 0)

  })


}


function third() {

  console.log("3")

}


async function run() {


  first();

  await second()

  third();

}


run();


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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