Node.js · Senior · Question 70 of 75
The client disconnects while your handler runs a slow database query. How do you avoid wasting work?
- ✓Correct answer: Listen for the response closing early (
res.on("close")with!res.writableFinished) and abort downstream work through anAbortSignal - 2Nothing can be done until the query finishes
- 3Node.js cancels database queries automatically
Explanation
Pass the signal to fetch, timers and libraries that support it. For databases, use query timeouts or cancellation if the driver supports them.