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

Add type signature for Promise.allSettled() #8230

Closed

Conversation

shau-kote
Copy link

@shau-kote shau-kote commented Dec 14, 2019

I miss the type signature for the new (ES2020) static method of the built-in Promise so I've decided to add it.

Documentation:

The change closes one of many items in #560

@jamesisaac jamesisaac added the Library definitions Issues or pull requests about core library definitions label Dec 20, 2019
@nmote nmote self-assigned this Mar 3, 2020
Copy link
Contributor

@nmote nmote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good, and it appears to match the spec. However, I'd like to see some tests added just to make sure Flow behaves the way we expect for these types.

I think they belong in tests/core_tests. Update the test output for this test but don't worry about re-recording the rest -- I can do that when I import.

lib/core.js Outdated
static race<T, Elem: Promise<T> | T>(promises: Iterable<Elem>): Promise<T>;
}

// we use this signature when typing await expressions
declare function $await<T>(p: Promise<T> | T): T;

declare function $settlement<T>(p: Promise<T> | T): SettlementResult<T>;
declare type SettlementResult<T> = { status: 'fulfilled', value: T } | { status: 'rejected', reason: any };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few requests for this line:

  • These types ought to be exact -- use {| and |} instead of { and } (this will be the default soon anyway).
  • This will be more usable if the type parameter is covariant, so write SettlementResult<+T> instead of SettlementResult<T>.
    • This will also force you to make at least the value property covariant by adding a + in front of it. And you might as well just make the other fields covariant too, while you're at it.
  • I'd rather see this use mixed for the rejection reason instead of any

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the substantive feedback! I've made a changes on all the items.

@nmote
Copy link
Contributor

nmote commented Mar 3, 2020

cc @moroine

@shau-kote
Copy link
Author

@nmote Thanks for the hint about tests. I am not sure I get the tests idea right, but hope I wrote them in the right way. :)

@nmote
Copy link
Contributor

nmote commented Mar 10, 2020

Thanks for updating this! Just today, somebody else who wanted to use Promise.allSettled put together a similar change, and it looks like their change will make it in first. I'll comment here when it lands. It wouldn't hurt to have some additional tests, though. If you are interested I'd be happy to work with you to get your tests in as well.

@nmote
Copy link
Contributor

nmote commented Mar 10, 2020

efc59d3

Leaving this open in case you'd like to add your tests

@shau-kote
Copy link
Author

Well, I am always glad to help by all means. :)

Am I getting you right, you are suggesting me to resolve the conflict between the two implementations (in favour of the already merged one) and add my tests to the existing ones? It seems reasonable.

Which way is preferred -- merging the master to my branch or rebasing my branch to the current master?

@mroch mroch added the ES2020 label Apr 16, 2020
@nmote nmote removed their assignment Mar 25, 2022
@SamChou19815
Copy link
Contributor

Closing this since it hasn't been updated for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed ES2020 Library definitions Issues or pull requests about core library definitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants