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 done very easily through a try Catch statement:

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

In asynchronous code, however, 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 running context for it to throw.

All, special parameters or error callback functions are typically used to represent exception information in asynchronous APIs. 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. An exception callback function is also accepted at the same time. 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);});

First glance looks. The above code is a bit messy. So we can consider extracting error handling in a way that uses named callback functions:

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. Another style of exception handling API. It will use the error message as the first parameter of the callback function. Assuming no error occurs, the value of the parameter is a Falsy value such as Null,undefine. Use this method for exception handling, as seen in the following:

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);    }); });});

In order to add readability to the code. A very large number of developers omit braces for the IF statement block that infers whether an exception needs to be handled:

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 the asynchronous API. Forgetting to deal with exceptions is often a mistake. This causes the exception information to be ignored, resulting in a worse 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 used 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

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.