Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from May 1, 2019

Conversation

uhyo
Copy link
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

@DanielRosenwasser DanielRosenwasser merged commit c9eb846 into microsoft:master May 1, 2019
@uhyo uhyo deleted the fix-issue-26586 branch May 2, 2019 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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