L
L
low molecular macro2021-09-13 20:57:27
Express.js
low molecular macro, 2021-09-13 20:57:27

Why does the function execute immediately despite await inside?

The answer to this question on StackOverflow is unclear.

I quote with translation:

The reason is catchAsync, the createTokens function returns the result immediately, despite the asynchronous Token.create inside. After its execution, res.json is launched, and res.cookie is executed only later, after Token.create is finished. This is what leads to the error.


It's not clear to me how catchAsync can cause this behavior. Why not wait until the end ? res.status(200).json({await jwtHelper.createTokens(user._id, res)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2021-09-13
@molekulyarniy

If you look at the catchAsync source (which is exactly 5 lines (!)), you can see that it does not return anything, i.e. returns undefined and await honestly waits for it and moves on.
https://github.com/hoangman1108/catch-async-expres...
In general, this “library” is designed to wrap the express middleware and using it for other purposes without understanding is fraught with such stupid mistakes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question