-
Notifications
You must be signed in to change notification settings - Fork 8
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
init(ts/hooks/useApiCall): add hook for consuming api promises #2533
Conversation
Coverage of commit
|
d1f33c4
to
2442cf5
Compare
Coverage of commit
|
Coverage of commit
|
7a1b995
to
e7c4c6d
Compare
Coverage of commit
|
e7c4c6d
to
c4f8bf8
Compare
Coverage of commit
|
reject = rej | ||
}) | ||
|
||
if (resolve === undefined || reject === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to understand this. Will this non-deterministically detect a race condition (or something) where resolve
or reject
get set to undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just to make typescript happy since I didn't have access to Promise.withResolvers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't actually know why I don't have access to that, especially in a test environment, but I wasn't getting anywhere and didn't want to spend more time on it. I assume it has something to do with the package.json or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh uh, I don't think I need this anymore since I tried to give resolve
and reject
default values to try and get rid of this if
statement. Not sure what would be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh I see - it's to get Typescript to recognize those values as not-undefined
!!
This is wounding my brain, but is there any way to know that the function passed in to Promise
's constructor will get called before the if statement happens?
I guess I dunno whether the if statement is better, or whether the default values are better... if there's some surprise and resolve
/reject
don't get initialized, then the test will be easier to debug with the if statement (cause it'll fail there), although I would perhaps update the message to be something more verbose than Error("Bad")
😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😭
const PromiseWithResolvers = <T>() => {
let resolve: undefined | ((v: T) => void) = undefined
let reject: undefined | ((v: any) => void) = undefined
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
if (resolve === undefined || reject === undefined) {
throw Error("Bad")
}
return { promise, resolve, reject }
}
tests/hooks/useApiCall.test.ts:130:7 - error TS2349: This expression is not callable.
Type 'never' has no call signatures.
130 resolve("first result")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gosh I wish we had Promise.withResolvers
😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay actually I ended up fixing this! https://github.com/mbta/skate/compare/3628116696376489700efc70f4d5af4bf2057657..1c5eb538128a89c5f662396f834f5c373e278e60
Coverage of commit
|
bc8891e
to
4ce2ae1
Compare
Coverage of commit
|
4ce2ae1
to
b767ac5
Compare
Coverage of commit
|
b767ac5
to
78a697d
Compare
Coverage of commit
|
78a697d
to
3628116
Compare
Coverage of commit
|
…above Co-authored-by: Josh Larson <jlarson@mbta.com>
3628116
to
1c5eb53
Compare
Coverage of commit
|
This adds a generic hook for consuming Promise's, we currently only have a need for this with API results.
This will be combined with #2531 to make implementing API endpoints which may return an error easier.
You can see this code in use on the PR #2534