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 isPromiseType. #29

Closed
wants to merge 1 commit into from
Closed

Conversation

heathkit
Copy link

Returns true if a type is a union type that includes Promise, or a type
that extends Promise.

Returns true if a type is a union type that includes Promise, or a type
that extends Promise.
@ajafff
Copy link
Owner

ajafff commented Jan 26, 2018

Thank you for the PR @heathkit.

I'm reluctant to add this function as is, because it's a very limited approach. This looks a lot like the code from TSLint's await-promise rule.
Let's ignore the licensing and copyright difficulties for now.
I'm wondering what your use case is and what you are trying to achieve with this function?

I'm asking because in the meantime I wrote two different implementations in another project. They are more spec compliant than this version. Both of them have a different purpose:

This one checks if a type that is already used with await could be a PromiseLike:
https://github.com/ajafff/wotan/blob/7adec1ac59fd9318f3e4f8cb43d373cb271af018/src/rules/await-promise.ts#L41-L58

This one checks if a type is really a PromiseLike by checking for a then method that takes a callback as first parameter:
https://github.com/ajafff/wotan/blob/7adec1ac59fd9318f3e4f8cb43d373cb271af018/src/rules/try-catch-return-await.ts#L92-L119

If I wanted to add one of these functions to tsutils, it would definitely be the second.

@heathkit
Copy link
Author

It's kind of a long story, but I'm trying to deal with the Selnium WebDriver promise manager being removed (see SeleniumHQ/selenium#2969).

We have a lot of code that used to be synchronous, for which we'll now need to ask people to put await in all the right places. We're worried that knowing which parts of WebDriver's API are now async isn't intuitive, especially for people who are used to the synchronous API. Also, a missing await in a WebDriver test can be painful to debug. We thought we'd save people some heartache by making a check that will warn if there are promises in a given block that are unused - not passed to a function, awaited, or assigned to a variable.

Either of those functions seem fine. I'm just trying to catch promises that aren't awaited, but probably should be.

@ajafff
Copy link
Owner

ajafff commented Jan 26, 2018

promises in a given block that are unused - not passed to a function, awaited, or assigned to a variable.

That sounds like you want to use TSLint's no-floating-promises, which does exactly what you want, instead of writing your own rule.

Anyways, I'll add my implementation of isPromiseLike (from my second link above) to the next version of this library.

@ajafff ajafff closed this in 4fe901d Feb 1, 2018
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.

2 participants