Description
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:
-
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 containPromise
, or the user could be required to useawait Promise.resolve
instead of justawait
. -
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 useawait Promise.resolve
-
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.