To from ES6 (ES2015) Formal norms, promise received high attention, has been no time to brief a wave, today to arrange a good thing promise.
Basic all browsers support promise, so be assured bold use.
First, the console prints down what the promise is, as shown in the following figure:
Promise is actually a constructor, which contains a lot of behavioral methods (Race,reject,resolve, etc.), in the prototype prototype then, catch and other callback methods.
Use scenarios like APAB foreground model operation Interaction:
var Omodel = new Sap.ui.model.odata.ODataModel (
"/sap/opu/odata/sap/zgs_zicerp_f4_srv/", {
json:true,
Loadmetadataasync:true
});
omodel.attachmetadataloaded (function (e) {
var path = "/help_ecuserset (' 1 ')"
omodel.read (Path,
{
Spath:path,
context:this,
success:function (odata,response) {
abc (oData);
},
error: function (oerror) {
ZICERP_F4.js.publicFunction.onError (oerror);
},
async:true
});
this);
Model of the asynchronous request after the interaction has success and error two callback, after the implementation of successful walk success, failure error. The industry has a saying: "The programmer is God, to the business process to specify the trajectory of life", the individual feel just right.
Because JavaScript execution is single-threaded, it is not avoidable to encounter callbacks in complex business scenarios. Promise is a business scenario that applies to callbacks, and although we can use a callback without it, the drawback is that the code becomes very redundant when it comes to multiple callbacks.
The Promise chain call (function execution depends on the result of the last function) to execute the callback function gives us a lot of convenience, on the code:
New Promise (function (resolve, reject) {//Wait (pending), completed (fulfilled), rejected (rejected)
resolve (10); New Promise Object
})
. Then (function (num) {console.log (' first: ', num); return num * 2;})
. Then (function (num) {console.log (' second: ', num); return num * 2;})
. Then (function (num) {Console.log (' third: ', num);})
. catch (function () {});
Execution results:
More scenarios are the packages that instantiate a Promise object in a specific execution function and plan the then function toward the result of the function running:
Function F1 () {
try{
var p=new Promise (function (resolve,reject) {
var num = Math.Round (math.random () *10); /Take 0 to 10 random integer
if (num <= 5) {
//Specify Resolve Status
resolve (num);
} else{
//Specify Reject status
reject (num);
}
);
return p;
} catch (Err) {
console.log (err.message);
}
}
F1 (). Then (function (data) {
//resolve
console.log (Data + ": To resolve")
},function (res,data) {
// Reject
Console.log (res + ": Value greater than 5 is reject")
});
Execution results:
When the program specifies that the generated random integer is greater than 5, the specified promise state is reject, less than 5 is the resolve state, and of course the multi-level callback continues in F1 (). Then (). Then (). Then () ...
Is that what promise is all about? Of course not, so 牜 things must have a lot of content, race, finally, all, catch and front-end engineers have played in jquery promise and so on, these are not played in the actual project, so the notes continue to improve behind. No need to know, a use will know. Limited level, welcome to exchange The last wish: work well