Reputation: 435
The specific function that I want to catch errors is from the Node.JS HTTP module:
response.write(data);
This function does not return a promise and is asynchronous so putting it in a try/catch doesn't work either. It does take an optional callback argument but this is only called on success of the operation. I also can't use await as this is function is not defined as an async function.
What is the best method for handling errors thrown by this function?
Upvotes: 0
Views: 31
Reputation: 664474
From the documentation:
The
write()
method […] calls the suppliedcallback
once the data has been fully handled. If an error occurs, thecallback
may or may not be called with the error as its first argument. To reliably detect write errors, add a listener for the'error'
event.
Upvotes: 1