-
-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
How to use async/await in express 5? #4256
Comments
Hey there, I won't comment on the "right way to use async/await" outside the context of Express v5, as you're best served by reading about async/await itself in other places. But I am happy to talk about it in the context of Express 5. There is one main difference between v4 and v5 when it comes to async/await and promises in general. In v5, if you return a promise from a response handler (or middleware), if that promise rejects and is not handled elsewhere, then Express will handle the error. It handles the rejection by passing the rejection reason to Look at these examples, they are functionally equivalent: v4 example: app.get('/test', function(req, res, next) {
return db().then(result =>{
// handle successful result
}).catch(err => {
// handle rejected promise
next(err)
})
}) v5 example: app.get('/test', async function(req, res, next) {
const result = await db()
// handle successful result
}) Both return a promise directly from the handler function. In the v4 example, the return is explicit (and we are assuming In the v4 example, we must manually catch the error and pass it to The point of the example is to show the different behavior between v4 and v5. To answer 2, async functions can indeed still use For 3, the simple answer is to use try/catch blocks, and decide how to handle errors yourself within the function. That is not strictly necessary, because if any single one of the In the example v4 code you posted, you are not explicitly handling any errors currently (besides the error returned from There are some subtle control flow differences between your v4 and v5 code, due to the use of await. Specifically the response in the else block |
@jonchurch Thank you very much. I asked this question because my test app often crashes and doesn't respond the connection. At first, I thought the reason was express didn't support async and await very well. After nearly three weeks of exploration, I knew that the reason was that there was no error handling in my code and cause the server crash. Your error handling differences between V4 and V5 are really good articles, and they benefit a lot, Thank you very much. |
@vipkouyu I'm happy I could help, if you have any other questions feel free to ask 👍 |
Here is my express 4.X code as follows:
I want to use async/await in express 5, but i don't find a example anywhere. As the following code, the mysql part has no problem(I have used bluebird to return a promise in mysql.js) ,but the other part code i think have a lot problem.
1、Is the right way to use async function?
2、In the async function, you can't use .then function(req.login)?
3、How to catch errors?
The text was updated successfully, but these errors were encountered: