javascript - 为什么等待函数会导致 "Invalid left-hand side in assignment"?

标签 javascript promise async-await

为什么await会专门导致无效的左手赋值?

   for(let b = 0; b < uniqueMenuItems.length; b++ )
    {
      uniqueMenuItems[b] = uniqueMenuItems[b].replace(/®.*/, "");
      await drinks[b] = I.scrape({"xpath":".//*[text()=\"" + uniqueMenuItems[b] + "\"]//following::*[@class=\"productControllers custom-product-ctrls\"][1]/div/div/select"}, uniqueMenuItems[b]);
    }

此代码出现错误:“await Drinks[b] = I.scrape(”

当不等待运行时,drinks 变量充满了promise,这不是我期望的,我希望 I.scrape 返回字符串数据。

I.scrap 这样做:

  async scrape(locator, uniqueMenuItem, I) {
    let driver = this.helpers.Protractor.browser;
    let pricingCalories = [""];
    let selectDropdowns = [""];
    let elementExists;
    //Look for options under the item name, if avilable loop all options and process base price and calories
    driver.element(locator).isPresent().then((result) => {
      //console.log(elementExists); //debug element existance results
      if (result) {
        console.log("Dropdown options exist for locator" + locator);
        selectDropdowns = this.getElementsText(locator);
        for(let a = 0; a < selectDropdowns.length; a++){
          console.log(a);
          I.selectOption({"xpath":"" + locator + ""}, selectDropdowns[a]);
          pricingCalories[a] += selectDropdowns[a];
          pricingCalories[a] += this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"productPrice ng-scope\"][1]"}) + " ";
          pricingCalories[a] += " " + this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"caloriesInfo ng-scope\"][1]"}) + " ";
        }
        return pricingCalories.toString();
      }
      //if select options are unavilable process the visible price and calories 
      else{
      console.log('No options, attempting collection of single item price and calorie data');
      pricingCalories[0] = this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"productPrice ng-scope\"][1]"})  + " ";
      pricingCalories[0] += " " + this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"caloriesInfo ng-scope\"][1]"}) + " ";
      return pricingCalories[0].toString();
      }
      });
  } 

最佳答案

您正在尝试await Drinks[b](其中drinks[b]被视为 promise ),然后分配I.scrape... code> 的结果。

您无法对表达式求值的结果进行赋值。

您可能想要等待 promise I.scrape... 的结果,然后将 that 分配给 drinks[b] 在这种情况下你需要:

drinks[b] = await I.scrape(

关于javascript - 为什么等待函数会导致 "Invalid left-hand side in assignment"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55304153/

相关文章:

javascript代码在运行后自行删除

php - 如何让 PHP 执行停止,直到采取某些操作

javascript - AngularJS 和 Typescript 的 Promise

javascript - 如何修改以下回调函数以使其返回 promise ?

javascript - 使用 async/await 在 fs.writeFile 之后执行代码

javascript - 正则表达式 - 匹配一个没有空格的字符串

javascript - 使用 jquery hover 增加 div 的不透明度

javascript - 重构功能和更好的 promise 链

c# - 返回已完成任务的异步方法出乎意料地慢

c# - 运行异步 lambda 时,使用 Task.Run(func) 还是 new Func<Task>(func)()?