Skip to content

Improve error message for use of 'await' in non-async context#31194

Merged
DanielRosenwasser merged 6 commits into
microsoft:masterfrom
uhyo:fix-issue-26586
May 1, 2019
Merged

Improve error message for use of 'await' in non-async context#31194
DanielRosenwasser merged 6 commits into
microsoft:masterfrom
uhyo:fix-issue-26586

Conversation

@uhyo
Copy link
Copy Markdown
Contributor

@uhyo uhyo commented May 1, 2019

As suggested in #26586, this PR adds to such an error a related span which points to its enclosing (non-async) function.

Example

// test.ts
function foo() {
  await null;

  for await (const _ of []);
}

Previous behavior:

test.ts:2:3 - error TS1308: 'await' expression is only allowed within an async function.

2   await null;
    ~~~~~

test.ts:4:7 - error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator.

4   for await (const _ of []);
        ~~~~~


Found 2 errors.

New behavior:

test.ts:2:3 - error TS1308: 'await' expression is only allowed within an async function.

2   await null;
    ~~~~~

  test.ts:1:10
    1 function foo() {
               ~~~
    Did you mean to mark this function as 'async'?

test.ts:4:7 - error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator.

4   for await (const _ of []);
        ~~~~~

  test.ts:1:10
    1 function foo() {
               ~~~
    Did you mean to mark this function as 'async'?


Found 2 errors.

Fixes #26586

Comment thread src/compiler/checker.ts Outdated
@DanielRosenwasser DanielRosenwasser requested a review from sandersn May 1, 2019 18:39
@DanielRosenwasser DanielRosenwasser merged commit c9eb846 into microsoft:master May 1, 2019
@uhyo uhyo deleted the fix-issue-26586 branch May 2, 2019 01:33
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Related error spans on non-async functions containing 'await'

2 participants