Skip to content

Should awaiting a non-Promise value be an error? #8310

Closed
@Arnavion

Description

@Arnavion

TypeScript Version:

1.9.0-dev.20160426

Code

var x = await 5;

Currently this compiles just fine and behaves correctly at runtime. But is there a situation where the user would want to deliberately await a non-Promise? Or should it be a compiler error?


I thought of:

  1. Normalizing a sync-or-async value:

    declare function mayReturnSyncOrAsyncResult(): number | Promise<number>;
    
    var result = await mayReturnSyncOrAsyncResult(); // number

    Either await could still be allowed for unions that contain Promise, or the user could be required to use await Promise.resolve instead of just await.

  2. Extracting the value of a thenable:

    declare function foo(): bluebird.Promise<number>;
    
    var result = await foo(); // number

    Again await could still be allowed, or the user could be required to use await Promise.resolve

  3. Deliberately introducing a yield point to make the rest of the function be a separate async continuation. I'm not sure if such a use actually exists in the wild. If it does exist, it too can be handled by await Promise.resolve

The downside of await Promise.resolve is that it introduces one extra Promise, since await will wrap the awaited value in one anyway.

I'm not completely convinced one way or the other. Opening this issue to get TS team's and other people's thoughts.


Alternatively I suppose we can make a tslint rule for "stricter awaits" or something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions