can we not read body after using await? #1178
Replies: 1 comment 5 replies
-
The issue you're describing with uWebSockets.js, based on the discussion context from the GitHub issue #1178, stems from the way uWebSockets.js handles HTTP request bodies in combination with asynchronous operations like Problem ExplanationIn uWebSockets.js, the HTTP request body is streamed and must be read synchronously within the request handler. When you use Expected BehaviorYes, this is expected behavior in uWebSockets.js. The library’s documentation (and community discussions) indicates that the request body should be read immediately in the handler, typically using the Would Callbacks Work Instead?Using callbacks instead of For example, a correct approach might look like this: app.post('/endpoint', (res, req) => {
let body = '';
// Read the body synchronously using onData
res.onData((chunk, isLast) => {
body += chunk.toString();
if (isLast) {
// Now perform async operations, e.g., DB call
someDbCallWithCallback((dbResult) => {
// Process headers or DB result
res.write(Buffer.from('Response'));
});
}
});
}); Here, the body is fully read before any asynchronous operations are performed, avoiding the issue you encountered. Why Async/Await Causes ProblemsWhen you use Solutions
Additional Notes
RecommendationTo confirm the exact behavior or potential workarounds, you could:
If you need me to search for updates on the GitHub issue or provide a more tailored code example, let me know! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I tried doing some checks based on headers, which needed a DB call with await. After that I was not able to read body, and it got stuck.
Is that expected behaviour?
would it have worked if I used callbacks instead of async/await keywords?
Beta Was this translation helpful? Give feedback.
All reactions