Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-4123

"await" should only be used with promises

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Refactor this redundant 'await' on a non-promise.
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, Sonar way recommended
    • Covered Languages:
      JavaScript, TypeScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1min
    • Analysis Scope:
      Main Sources, Test Sources
    • TSLint:
      await-promise

      Description

      It is possible to use await on values which are not Promise}}s, but it's useless and misleading. The point of {{await is to pause execution until the Promise's asynchronous code has run to completion. With anything other than a Promise, there's nothing to wait for.

      This rule raises an issue when an await}}ed value is guaranteed not to be a {{Promise.

      Noncompliant Code Example

      let x = 42;
      await x; // Noncompliant
      

      Compliant Solution

      let x = new Promise(resolve => resolve(42));
      await x;
      
      let y = p ? 42 : new Promise(resolve => resolve(42));
      await y;
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            carlo.bottiglieri Carlo Bottiglieri (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: