microsoft / microsoft/TypeScript

Suggestion: add flag to notify when awaiting a non-promise

Open
#41,277 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

async promise force error

Suggestion

Add a tsconfig.json flag to display info/warning/error when awaiting non-PromiseLike code. This will catch developer errors.

Use Cases

Catch developer errors when writing async code.

Example

The following code is valid:

await Promise.all(urls.map(url => {
  fetch(url);
});
console.log('fetching complete');

The developer intends to do something once all the fetches complete, but since map is not returning any value, it ends up awaiting [undefined, undefined, ...], which means fetching complete is logged before the fetches complete. It would be nice if TS gave an info/warning/error in this case.

The correct code is:

await Promise.all(urls.map(url => {
  return fetch(url);
});
console.log('fetching complete');

This example is paired down to the bare minimum for simplicity, but IRL code is more complex.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is identified in the issue, so start by locating TypeScript's handling of await expressions and tsconfig diagnostics. Done means defining and implementing a configurable info, warning, or error for awaiting non-PromiseLike code, with coverage for the provided Promise.all example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.