简体   繁体   English

为什么我没有收到以下所有代码片段的“冗余返回等待”(ESLint:no-return-await)警告?

[英]Why am I not getting "Redundant return await" (ESLint: no-return-await) warning for all the below code snippets?

I have below code paths which are giving the Redundant return await warning in "IntelliJ": ( https://eslint.org/docs/rules/no-return-await )我有以下代码路径,它们在“IntelliJ”中给出了冗余返回等待警告:( https://eslint.org/docs/rules/no-return-await

Note: I see -ve votes for my question.注意:我看到 -ve 为我的问题投票。 Most likely I am missing something basic - can you point out in comments/question what am I missing?我很可能遗漏了一些基本的东西——你能在评论/问题中指出我遗漏了什么吗? I am spending a significant amount of time asking the question - it will be helpful if you add details for -ve votes.我花了大量时间问这个问题——如果你为 -ve 投票添加细节会很有帮助。

 import {browser, By, element, promise, protractor} from 'protractor';
          async randomMethod(): Promise<string> {
            return await element.all(By.css('.random-class')).first().getText();
          }

 async randomMethod4()  {
     return await browser.driver.get(this.url);
  }

Please note that just removing the await in the above methods get rid of the warning (like following code snippet).请注意,只需删除上述方法中的 await 即可消除警告(如以下代码片段)。 But my question is why is it ok for the below methods to do "return await", but not for "above".但我的问题是为什么下面的方法可以做“返回等待”,而不是“上面”。

async randomMethod(): Promise<string> {
        return element.all(By.css('.random-class')).first().getText();
      }

Whereas the following code paths don't give the same warning:而以下代码路径不会给出相同的警告:

  async randomMethod2(): Promise<number> {
    return await element.all(By.css('.mat-row')).count();
  }

  async randomMethod3() {
    return await element.all(By.css('.random-class')).first();
  }

I am trying to understand in detail the reason for warning even though my tests are passing as I may be missing something subtle here.我试图详细了解警告的原因,即使我的测试通过了,因为我可能在这里遗漏了一些微妙的东西。 Any thoughts are helpful.任何想法都有帮助。

return await element.all(By.css('.random-class')).first().getText();

Btw, following are the function definitions/prototypes:顺便说一句,以下是函数定义/原型:

first(): ElementFinder;

count(): wdpromise.Promise<number>;

getText(): promise.Promise<string>;

get(url: string): promise.Promise<void>;

async functions always return a Promise, even if you don't await inside them. async函数总是返回一个 Promise,即使你没有在它们里面等待。 Since they always return a Promise you always have to "unwrap them" at the calling site (either by using await or .then()) in order to get the value or do something when it resolves.由于它们总是返回一个 Promise,因此您总是必须在调用站点(通过使用 await 或 .then())“解开它们”,以便在解决时获取值或执行某些操作。

When you use return await you are "uwrapping" a promise only for the value to be "wrapped" again (since you are inside an async function), so it is always redundant.当您使用return await您只是在“包装”一个承诺,只为再次“包装”该值(因为您在async函数中),因此它始终是多余的。

It does not affect the behavior of your code, which is why your tests still pass.它不会影响您的代码的行为,这就是您的测试仍然通过的原因。

Edit:编辑:

After reading a bit on the no-return-await rule from eslint, turns out there is one case in which it is not redundant, which is inside a try-catch block.在从 eslint 阅读了一些关于no-return-await规则之后,结果证明在一种情况下它不是多余的,它位于 try-catch 块内。

async function foo() {
    try {
        return await bar();
    } catch (error) {}
}

the await is necessary to be able to catch errors thrown from bar() . await是必要的,以便能够捕获从bar()抛出的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM