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

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

等待僅在異步功能中有效

等待僅在異步功能中有效

慕妹3242003 2019-11-03 08:04:53
我在 lib/helper.jsvar myfunction = async function(x,y) {   ....   reutrn [variableA, variableB]}exports.myfunction = myfunction;然后我試圖在另一個(gè)文件中使用它 var helper = require('./helper.js');    var start = function(a,b){     ....     const result = await helper.myfunction('test','test'); } exports.start = start;我有一個(gè)錯(cuò)誤“等待僅在異步功能中有效”有什么問(wèn)題
查看完整描述

3 回答

?
qq_花開(kāi)花謝_0

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

錯(cuò)誤不是指myfunction而是start。


async function start() {

   ....


   const result = await helper.myfunction('test', 'test');

}

// My function

const myfunction = async function(x, y) {

  return [

    x,

    y,

  ];

}


// Start function

const start = async function(a, b) {

  const result = await myfunction('test', 'test');

  

  console.log(result);

}


// Call start

start();

我利用這個(gè)問(wèn)題的機(jī)會(huì)來(lái)告訴你一個(gè)已知的反模式的使用await方法:return await。


錯(cuò)誤


async function myfunction() {

  console.log('Inside of myfunction');

}


// Here we wait for the myfunction to finish

// and then returns a promise that'll be waited for aswell

// It's useless to wait the myfunction to finish before to return

// we can simply returns a promise that will be resolved later

async function start() {

  // useless await here

  return await myfunction();

}


// Call start

(async() => {

  console.log('before start');


  await start();

  

  console.log('after start');

})();

正確


async function myfunction() {

  console.log('Inside of myfunction');

}


// Here we wait for the myfunction to finish

// and then returns a promise that'll be waited for aswell

// It's useless to wait the myfunction to finish before to return

// we can simply returns a promise that will be resolved later

async function start() {

  return myfunction();

}


// Call start

(async() => {

  console.log('before start');


  await start();

  

  console.log('after start');

})();



查看完整回答
反對(duì) 回復(fù) 2019-11-04
?
手掌心

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

當(dāng)我收到此錯(cuò)誤時(shí),事實(shí)證明我在“異步”函數(shù)中調(diào)用了map函數(shù),因此此錯(cuò)誤消息實(shí)際上是指未標(biāo)記為“異步”的map函數(shù)。通過(guò)從map函數(shù)中取消“ await”調(diào)用并提出了一些其他實(shí)現(xiàn)預(yù)期行為的方法,我解決了這個(gè)問(wèn)題。


var myfunction = async function(x,y) {

    ....

    someArray.map(someVariable => { // <- This was the function giving the error

        return await someFunction(someVariable);

    });

}



查看完整回答
反對(duì) 回復(fù) 2019-11-04
?
qq_笑_17

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

真正的問(wèn)題是您需要了解異步/等待如何在這里工作。錯(cuò)誤在于start function()


您需要定義功能Async函數(shù)的使用await作為


await 使用承諾/未來(lái)/任務(wù)返回方法/功能


async 將方法/功能標(biāo)記為能夠使用等待。


Await實(shí)際上是在執(zhí)行promise / resolve的相同過(guò)程,并且由于該功能是async其他任務(wù),因此正在并行處理


有關(guān)更多信息,請(qǐng)參考 MDN DOCS


async function foo(){


let myAsyncCall = await .... ('/endpoint') // hitting on some api

console.log(myAsyncCall) // myAsyncCall will log out whenever the request get resolved


}


foo()



查看完整回答
反對(duì) 回復(fù) 2019-11-04
  • 3 回答
  • 0 關(guān)注
  • 309 瀏覽
慕課專欄
更多

添加回答

舉報(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)