Tristan Warren
Tristan Warren

Reputation: 435

How do I catch an asynchronous error from a function without promises?

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

Answers (1)

Bergi
Bergi

Reputation: 664474

From the documentation:

The write() method […] calls the supplied callback once the data has been fully handled. If an error occurs, the callback 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

Related Questions