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

Document the use of async functions in hooks. #1593

Closed
mcollina opened this issue Apr 13, 2019 · 19 comments
Closed

Document the use of async functions in hooks. #1593

mcollina opened this issue Apr 13, 2019 · 19 comments
Labels
bug Confirmed bug documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@mcollina
Copy link
Member

🐛 Bug Report

The current documentation is incorrect on how hooks works, as we state:

  • preValidation(request, reply, done): a function called after the shared preValidation hooks, useful if you need to perform authentication at route level for example, it could also be an array of functions.

We need to document that async preValidation(request, reply) is also correct, but async preValidation(request, reply, done) is not.

@mcollina mcollina added bug Confirmed bug good first issue Good for newcomers labels Apr 13, 2019
@delvedor delvedor added the documentation Improvements or additions to documentation label Apr 13, 2019
@vcanales
Copy link

Wouldn't the following paragraph in Hooks.md address this issue?

Notice: the next callback is not available when using async/await or returning a Promise. If you do invoke a next callback in this situation unexpected behavior may occur, e.g. duplicate invocation of handlers.

@mcollina
Copy link
Member Author

Would you like to send a PR?

@vcanales
Copy link

Sorry for not being clearer: what I mean is that paragraph is already in the docs.

@Eomm
Copy link
Member

Eomm commented Apr 19, 2019

Sorry for not being clearer: what I mean is that paragraph is already in the docs.

He is referring to Routes.md

@mcollina
Copy link
Member Author

That is not sufficient as quite a few people have asked/made this mistake recently between gitter and help.

@delvedor
Copy link
Member

Wild idea: check at startup if is an async function and the arity, and if it is an async function with 3 arguments, throw an error.

@jsumners
Copy link
Member

FYI: I make this mistake myself routinely.

@trey-m
Copy link

trey-m commented Apr 30, 2019

I am experiencing this right now. What is the suggested way to implement multiple preHandlers, or any lifecycle hook, asynchronously?

@mcollina
Copy link
Member Author

Can you paste some code that is not working for you? Have you tried using an array?

@trey-m
Copy link

trey-m commented Apr 30, 2019

fastify.get("/", 
fastify.addHook('preHandler', async (request, reply) => {
  const one = await one(request, reply)
  const two = await two(request, reply)
  return
}),
async (request, reply) {
  code for handler
})

This is essentially what my endpoint looks like. This could very well be an anti-pattern as I am new to how the handlers work in fastify. I am open to any suggestions on a proper way of handling this.

@mcollina
Copy link
Member Author

You can call addHook twice, it should all work fine.

@trey-m
Copy link

trey-m commented Apr 30, 2019

Okay cool. I would ideally like to clean up the route though and extract that addHook logic. What is the suggested method on extracting that code so I could still stay in scope?

@mcollina
Copy link
Member Author

Just pass an option object with a “preHandler” property with an array and your two functions. Let me know if it works!

@trey-m
Copy link

trey-m commented Apr 30, 2019

Awesome, it's working as expected and much more elegant than having the addHook block directly in the handler. Appreciate all the help!

@mcollina
Copy link
Member Author

mcollina commented May 1, 2019

This doc needs to be improved ;).

@Eomm
Copy link
Member

Eomm commented Nov 2, 2019

Closing thanks to improved docs by @RafaelGSS 💪

@Eomm Eomm closed this as completed Nov 2, 2019
@PierBover
Copy link
Contributor

What about error managing in an async hook?

The hooks docs specify to use done(new Error('Some error')) for non async hooks but there is no info on error handling on an async hook.

https://www.fastify.io/docs/latest/Hooks/#manage-errors-from-a-hook

@Eomm
Copy link
Member

Eomm commented Apr 4, 2020

What about error managing in an async hook?

you need to throw new Error('something wrong'), would you like to add it to our docs?
I would be helpful!

@PierBover
Copy link
Contributor

PierBover commented Apr 4, 2020

Sure why not!

Edit:

#2176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

7 participants