Skip to content

Feature: require await (particularly return await) whereever promises are used #3576

@coolaj86

Description

@coolaj86

There's an entire class of error that can be categorically eliminated by always using await whenever there is a promise.

Consider this simple refactor:

async function doStuff() {
  return theOnlyFunctionIThoughtIWouldEverNeed();
}
async function doStuff() {
  let x = theOnlyFunctionIThoughtIWouldEverNeed();
  let y = doMoreThings(x); // error, expected 'x' to be a value, not a promise
  return y;
}

or this one, following the "return something, or return nothing" philosophy:

async function doStuff() {
  theOnlyFunctionIThoughtIWouldEverNeed();
}

which should instead be

async function doStuff() {
  await theOnlyFunctionIThoughtIWouldEverNeed();
}

Also, await lends clarity to the reader when it's non-obvious that the function being called is a promise / async function.

Hence the original should be:

async function doStuff() {
  return await theOnlyFunctionIThoughtIWouldEverNeed();
}

I would put this in the same category as curly: true in terms of its usefulness and the kinds of problems that it solves.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions