Answer the question
In order to leave comments, you need to log in
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.
res.status(200).json({
await jwtHelper.createTokens(user._id, res)
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question