-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Issues with async/await in handlers #9
Comments
Great work! As far as I know, when returning an async function (i.e. a function that retuns a promise) I'm pretty sure you shouldn't need to await. Could you give me a working example where this is not the case? I haven't run into this issue with my async handlers yet. For the second problem, I think this upcoming change will actually fix the issue since we are no longer returning the validator function in the context object, but a similar validation result object with just a shallow copy of the errors (I think). Can you verify? |
I mean I don't really see any problem with adding the awaits, but I'm just really curious to see why async work can't be performed. Not adding the await just seems better in my eyes so users have an option to do non-async handlers as well. export type Handler = (context?: Context, ...args: any[]) => any | Promise<any>; |
You're actually right for the first one, I feel kind of dumb now :'(. |
Yes I confirm that the second issue is fixed when running the code inside the PR. |
Great! Closing this issue now and publishing changes from #6. :) |
When playing with the library, I found two issues when using asynchronous code in my handlers. I'm using it with koa, but I think the problem would be the same for every framework.
First, the calls to the handlers are not awaited, meaning I can't do async work in the handler.
There is a possible fix here.
Second, its a really small one but since we only have one instance of the validation function, the validation errors can be lost in the case a async call is made in the error handler.
For example, is the following handler is registered as
validationFail
,c.validation.errors
in the body can contains a different error.This is obviously easy to fix like this:
That being said I made a small patch for this aswell.
Feel free to reuse any of this, and/or close the issue.
The text was updated successfully, but these errors were encountered: