【问题标题】:Where to put await in async function在异步函数中放置等待的位置
【发布时间】:2021-09-25 18:03:05
【问题描述】:

我正在尝试使用异步调用,但是每当我输入 await 命令字时,脚本就无法运行。 任何建议都欣然接受。

底部的替代代码基于提供的注释

    async function masterDeploy(PrimaryControl){
        //2. Collect learning event dates from programme
        LearnEvents = progLearnerEvent(progGuid)                                                                                
          .then(LearnEvents =>{
            console.log(LearnEvents)                                                                    
            for (var b = 0; b < LearnEvents.length; b++) {                                          
              retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if await placed at beginning of this line
              console.log(FlightPlanArray)  
              learnEventsArrComb.push(FlightPlanArray)
              console.log(learnEventsArrComb)
            }
            return learnEventsArrComb
          })
          .then(learnEventsArrComb => {
            console.log(learnEventsArrComb)
          })
          .catch(error =>
              {DisplayError(error)}
            );

另类从这里开始

        LearnEvents = progLearnerEvent(progGuid)    
            await (LearnEvents =>{
            //.then(LearnEvents =>{
              console.log(LearnEvents)                                                                  
              for (var b = 0; b < LearnEvents.length; b++) {                                            
                await retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if placed at beginning of this line
                console.log(FlightPlanArray)    
                learnEventsArrComb.push(FlightPlanArray)
                console.log(learnEventsArrComb)
              }
              return learnEventsArrComb
            })
            //.then(learnEventsArrComb => {
            //  console.log(learnEventsArrComb)
            //})
            //.catch(error =>
            //      {DisplayError(error)}
            //);
      
      }

【问题讨论】:

  • 首先将 .then() 调用替换为 await 语法。 然后你可以在循环中添加await
  • 您的意思是将 progLearnerEvent 承诺也重写为异步吗?
  • progLearnerEvent(progGuid) 已经返回了一个承诺,你不需要重写 progLearnerEvent 函数。你应该使用 await 而不是 .then() 的承诺。
  • 你是这个意思吗?
  • 并非如此。您需要 await 承诺,并且根本不使用回调。

标签: javascript async-await promise


【解决方案1】:

你可以这样做:

const masterDeploy = async (PrimaryControl) =>{
        //2. Collect learning event dates from programme
        LearnEvents = await progLearnerEvent(progGuid)                                                                                
          .then(LearnEvents =>{
            console.log(LearnEvents)                                                                    
            for (var b = 0; b < LearnEvents.length; b++) {                                          
             await retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if await placed at beginning of this line
              console.log(FlightPlanArray)  
              learnEventsArrComb.push(FlightPlanArray)
              console.log(learnEventsArrComb)
            }
            return learnEventsArrComb
          })
          .then(learnEventsArrComb => {
            console.log(learnEventsArrComb)
          })
          .catch(error =>
              {DisplayError(error)}
            );

试试这个...

【讨论】:

    【解决方案2】:

    调用masterDeploy函数时需要添加await

    function main(){ 
      await masterDeploy(info)
    }
    

    另外,我建议在您的代码中进行以下更改:

    async function masterDeploy(PrimaryControl){
    //2. Collect learning event dates from programme
    try{
        const LearnEvents = await progLearnerEvent(progGuid)  
        console.log(LearnEvents)                                                                              
        for (let b = 0; b < LearnEvents.length; b++) {                                                    retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if await placed at beginning of this line
            console.log(FlightPlanArray)  
            learnEventsArrComb.push(FlightPlanArray)
            console.log(learnEventsArrComb)
        }
    }
    catch(error){
        DisplayError(error)
    }
    }
    

    【讨论】:

    • 谢谢,虽然这段代码也没有运行,而且缺少 retLearnEvents 调用
    • "调用“masterDeploy”函数时需要添加await" - 没有。
    • masterDeploy 从 Microsoft Dynamics 功能区上的按钮调用。 PrimaryControl 是传递的上下文,以便可以访问记录中的参数。为简洁起见,我省略了此代码。我在星期一见到一位朋友,他将解释您的评论的含义@Bergi
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多