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

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

等待僅在異步功能中有效

等待僅在異步功能中有效

慕碼人2483693 2019-10-12 09:50:26
我在 lib/helper.jsvar myfunction = async function(x,y) {   ....   reutrn [variableA, variableB]}exports.myfunction = myfunction;然后我試圖在另一個文件中使用它 var helper = require('./helper.js');    var start = function(a,b){     ....     const result = await helper.myfunction('test','test'); } exports.start = start;我有一個錯誤“等待僅在異步功能中有效”有什么問題
查看完整描述

3 回答

?
慕的地6264312

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

錯誤不是指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();

我利用這個問題的機會來告訴你一個已知的反模式的使用await方法:return await。


錯誤


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');

})();


查看完整回答
反對 回復 2019-10-12
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

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


var myfunction = async function(x,y) {

    ....

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

        return await someFunction(someVariable);

    });

}


查看完整回答
反對 回復 2019-10-12
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

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


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


await 使用承諾/未來/任務返回方法/功能


async 將方法/功能標記為能夠使用等待。


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


有關更多信息,請參考 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()


查看完整回答
反對 回復 2019-10-12
  • 3 回答
  • 0 關注
  • 588 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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