Effective JavaScript Item 63 Note An exception that may be ignored in an asynchronous call

Source: Internet
Author: User
Tags try catch

Exception handling is a difficult point in asynchronous programming. In synchronized code, exceptions can be easily accomplished through a try Catch statement:

Try {    f ();    g ();    Catch (e) {    // handle any error that occurred ...}

But in asynchronous code, it is unrealistic to include all possible exceptions with a try code block. In fact, asynchronous API settings cannot throw exceptions. Because when an exception occurs, there is usually no execution context for it to throw. All, in asynchronous APIs, special parameters or error callback functions are typically used to represent exception information. For example, the exception handling for the downloaded file mentioned in item 61 can be done as follows:

Downloadasync ( "http://example.com/file.txt" ,function(text) {Console. Log( "File Contents: "  +text);},function(Error) {Console. Log( "Error: "  +(error);});

The visible downloadAsync method not only accepts a callback function as a successful return response, but also accepts an exception callback function. And so on, the exception handling when downloading multiple files is this:

Downloadasync ( "a.txt" ,function(a) {Downloadasync ( "b.txt" ,function(b) {Downloadasync ( "c.txt" ,function(C) {Console. Log( "Contents: "  +A+B+c); },function(Error) {Console. Log( "Error: "  +Error);    }); },function(Error) { // repeated error-handling logicConsole. Log( "Error: "  +Error); });},function(Error) { // repeated error-handling logicConsole. Log( "Error: "  +(error);});

At first glance, the above code is a bit messy. So we can consider using named callback functions to extract error handling:

function OnError(Error) {Console. Log( "Error: "  +Error);} Downloadasync ( "a.txt" ,function(a) {Downloadasync ( "b.txt" ,function(b) {Downloadasync ( "c.txt" ,function(C) {Console. Log( "Contents: "  +A+B+c); },OnError); },OnError);},OnError);

In node. js, there is also a style of exception handling API. It takes the error message as the first parameter of the callback function, and if no error occurs, the value of the parameter is a Falsy value such as Null,undefine. The exception handling in this way is as follows:

function OnError(Error) {Console. Log( "Error: "  +Error);} Downloadasync ( "a.txt" ,function(Error,a) {if(Error) {OnError(error);return; } downloadasync ( "b.txt" ,function(Error,b) { // duplicated error-checking logic        if(Error) {OnError(error);return; } downloadasync (Url3,function(Error,C) { // duplicated error-checking logic            if(Error) {OnError(error);return; } Console. Log( "Contents: "  +A+B+c);    }); });});

To increase the readability of the code, many developers omit the braces for the IF statement block that is used to determine if exception handling is required:

function OnError(Error) {Console. Log( "Error: "  +Error);} Downloadasync ( "a.txt" ,function(Error,a) {if(Error)return OnError(error); Downloadasync ( "b.txt" ,function(Error,b) {if(Error)return OnError(error); Downloadasync (URL3,function(Error,C) {if(Error)return OnError(error); Console. Log( "Contents: "  +A+B+c);    }); });});

When using asynchronous APIs, forgetting to handle exceptions is a frequent mistake. This causes the exception information to be ignored, resulting in a poor user experience. Therefore, when using the asynchronous API, remember to handle exception cases. I think that's why, in node. js, the exception information is treated as the first parameter of the callback function, so the developer has to face it.

Summarize
    1. Avoid copying and pasting exception handling code, which is a better choice for shared functions.
    2. Ensure that exception conditions are handled to avoid ignoring exceptions.

Effective JavaScript Item 63 Note An exception that may be ignored in an asynchronous call

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.