A
A
Andrey Prokopyuk2012-04-29 16:12:10
JavaScript
Andrey Prokopyuk, 2012-04-29 16:12:10

Error handling during asynchronous execution in JavaScript?

When executing code synchronously, exceptions are a very convenient error handling mechanism. But in JavaScript, there is a lot of code that runs asynchronously - for example, requests to the server. Often, callbacks are used to handle errors in this case, but in terms of convenience, this technique cannot be compared with exceptions, in which the call stack independently unwinds to the place of interception.
I searched long and hard for something on this topic, but found only something like this:

try {
    helloWorld();
}
catch (e) {
    alert(e.message);
}

function helloWorld() {
    throw new Error('Hello world!');
}

Are there any good articles on asynchronous error handling, especially in JavaScript? Maybe there are specific patterns or little-known features of the language?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mithgol, 2012-04-30
@Andre_487

An asynchronous function is therefore asynchronous because the return from it (return) occurs much faster than the source code of the operations and callbacks launched from inside it finishes executing.
Therefore, it is useless to wrap such a function in a try-catch construct (when the return from the function happened, then no errors could have occurred yet - which means there is nothing to catch yet), and involuntarily you have to use passing them by callback functions to handle errors, rather than throwing them throw statement.
For such a transfer, as a rule, the first parameter of the callback function is used, that is, its call has the form “ callback(err, data) ”, where error is an error or null,and data is the data sent if there is no error. (There can be more than one parameter with data.)
The reason for this order, in which the error parameter comes first in a row, is that instead of "throw new Error()", a simple callback function call with a single parameter is used: "callback(new Error ()); return;" - in general, it is not too difficult than throw.
To comply with this order, inside such a callback function , first, the first parameter is compared with null (which allows, if they are not equal, to immediately notice the error that has occurred, if necessary, immediately process it or pass it to the parent callback function),and only then they start processing the data received through the subsequent parameters of the function.
I do not share the opinion, according to which libraries of such functions, which are specifically designed to handle errors in asynchronous functions, are crooked and poorly made, and overfilled, and cannot be of help. I hold the opposite opinion: the async library seems to me a convenient tool for managing asynchronous functions in general, and in particular for handling errors in them.
And this is not only my opinion: I already noticed in January of this year (2012) that the async package was among the ten most used modules for the Node engine according to The Node Toolbox website, based on the statistics of the npm package manager.

I
Ilya Shabanov, 2012-04-29
@ishaba

Not exactly an answer to the question, but I personally prefer console.log() instead of alert()

R
Riateche, 2012-04-29
@Riateche

stackoverflow.com/questions/3677783/is-it-possible-to-catch-exceptions-thrown-in-a-javascript-async-callback
stackoverflow.com/questions/6620207/how-to-easily-handle-all-errors -in-asynchronous-functions-of-javascript The
easiest way is to wrap the contents of each function that is called asynchronously in a try/catch.

E
egorinsk, 2012-04-29
@egorinsk

For asynchronous calls, functions are written in a chain in the form:
callFunction(function() { /* async code */ }).then(function() { /* async code */ }).then(function(){ /* async code */ }).onError(function() { /* error handler */ });
Such a function organizes the order of execution and error handling itself. There are some libraries out there for this, but I'm sure they're all crooked, poorly made, and overbloated, so it's better to write your own code for that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question