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

Make the PromiseValue type handle nested promises #112

Merged
merged 9 commits into from Jun 27, 2020
Merged

Make the PromiseValue type handle nested promises #112

merged 9 commits into from Jun 27, 2020

Conversation

onichandame
Copy link
Contributor

As per this discussion, a more realistic use case of unwrapping a type from Promise is when the Promise is resolved to another Promise.

For example, await Promise.resolve(Promise.resolve(2)) is resolved as number, not Promise<number>. The current type PromiseValue however, does not reflect this behaviour.

Thanks to Gerrit0 who shared the trick, I stole his brilliant idea and brought to this package.

@sindresorhus
Copy link
Owner

Shouldn't PromiseValue just do this by default? Instead of a separate type.

@onichandame
Copy link
Contributor Author

  • reason 1: I thought it would be clearer for the reviewer if changes are isolated in new files

  • reason 2: Let me quote from the creator of this type, Gerrit0 said,

    You can work around this issue by using a recursive type alias, but be aware that this is officially not supported and may break in future versions.
    

    Therefore I think it would be better to keep a fallback type PromiseValue so that when this type is broken someday, the maintainer can simply delete this type or alias it to PromiseValue to workaround the possible fatal bugs.

  • reason 3: There may be some use cases where people do not want to recursively unwrap the promise(although I cannot think of any)

So I decided to post the changes first and see how people think about this.

@sindresorhus
Copy link
Owner

Are you aware that they plan to solve this problem in TypeScript itself? microsoft/TypeScript#27711 (comment)

@sindresorhus
Copy link
Owner

I doubt it will break in the future, but if so, we could probably find a workaround.

@onichandame
Copy link
Contributor Author

I did look at that issue. The problem is that the official support won't be available before TS 4.0. Before TS people have solved the problem, it is necessary to have this type as a temporary workaround.

I do wish to alter the behaviour of PromiseValue so that it unwraps recursively. So if you agree too, I will make another commit that implements it.

@sindresorhus sindresorhus changed the title Add PromiseValueRecursive type Make the PromiseValue type handle nested promises Jun 13, 2020
@sindresorhus sindresorhus merged commit 577c2a2 into sindresorhus:master Jun 27, 2020
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.

None yet

2 participants