facebook / facebook/flow

await/.then does not unwrap Promise<Promise<T>>

Open
#5,582 5 comments 5 reactions 0 assignees View on GitHub
Typing: async/await
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

While it is possible to create types like `Promise>`, `await` and `Promise#then` do not unwrap to `T` correctly the same way they do in JavaScript implementations.

[[try flow](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVATApgYxgQwCcswoBXAOxwBcBLOCsBQ-ABwB4AVAPgAooKALjCcAlMIAKhOAFtaAZyxduAbnT55ATyqlKNeoxn5aFXqLABvVGDAws1MADd8MSdLmL2U2QqUAWACZubjAAXiYWVl5vDywAOmJ5OBhHLF5A0VE1GzsHROFAsLB8BGMHZxhssGsnFzjqAAssU158sAywkKsbG2BgMDjBmoBfLNRh1CA)]

```js
declare function wrap(fn: T): Promise;

async function main() {
let val: Promise> = wrap(Promise.resolve(42));
let res: 42 = await val;

val.then((res: 42) => {
// ...
});
}

```

**Error one: `await`**

```
7: let res: 42 = await val;
^ Promise. This type is incompatible with
7: let res: 42 = await val;
^ number literal `42`
```

**Error two: `.then`**

```
9: val.then((res: 42) => {
^ function. This type is incompatible with the expected param type of
[LIB] static/v0.62.0/flowlib/core.js:598: onFulfill?: (value: R) => Promise | U,
^ function type
The first parameter is incompatible:
6: let val: Promise> = wrap(Promise.resolve(42));
^ Promise. This type is incompatible with
9: val.then((res: 42) => {
^ number literal `42`
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.