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