ANGULAR2 (iv)--promise

Source: Internet
Author: User
Tags exception handling

Angular life cycle Method:
Each method name is prefixed with the interface name Ng
Using the angular lifecycle requires implementing the appropriate interfaces to use their lifecycle methods

Promise asynchronous technology is registered using the then () method, and exception handling uses the catch () method
Create a Promise object:

Const P = new Promise (
    function (resolve, reject) {//(A)
        •
        · if (...) {
            resolve (value);//Success
        } else {
            reject (reason);//Failure
        }
    });

Use promise:

Promise
. Then (value => {/* Fulfillment/})
. catch (Error => {/* rejection/});

HTTP Promise Instance:
1. Create

function HttpGet (URL) {return
    new Promise (
        function (resolve, reject) {
            Const request = new XMLHttpRequest () ;
            Request.onload = function () {
                if (this.status = =) {
                    //Success
                    Resolve (This.response);
                } else {
  
   //something went wrong (404 etc.)
                    Reject (new Error (This.statustext));
                }
            ;
            Request.onerror = function () {
                Reject (new error (
                    ' XMLHttpRequest error: ' +this.statustext)
            };
            Request.open (' get ', url);
            Request.send ();
        });

  

2. Use

HttpGet (' Http://example.com/file.txt ')
. Then (
    function (value) {
        console.log (' Contents: ' + value);
    } ,
    function (reason) {
        console.error (' Something went wrong ', reason);
    });

Use the settimeout () method to delay

For most values x, it returns a Promise the IT fulfilled with X
promise.resolve (' abc ')
  . Then (x => Console.lo g (x)); ABC

//promise.reject (Err) returns a Promise that is rejected with err
const MYERROR = new Error (' problem! ');
Promise.reject (Myerror)
. catch (Err => console.log (err = = Myerror));//True

If you resolve the Promise Q returned by then () with a normal value, you can pick up this value via a subsequent then ():

Asyncfunc ()
. Then (function (value1) {return
    123;
})
. Then (function (value2) {
    console.log (value2);//123
});

Better Practice:
1.return Complete Promise

function foo () {return
    asyncfunc ()
    . Then (result => {• •
    });
}

2. Nested types

ASYNCFUNC1 ()
. Then (result1 => {return
    asyncFunc2 ();
})
. Then (result2 => {·
});

3. Do not create promise to create, when the promise parameter is not useful, you can pass then () for us to generate promise

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.