Because of the asynchronous mechanism of javascript, a common problem occurs, the callback pyramid:
loadImg('a.jpg', function() { loadImg('b.jpg', function() { loadImg('c.jpg', function() { console.log('all done!'); }); }); });
Promise Literally, promise. If A calls B, and B returns a promise to A, then A can write the plan like this: When B returns the result to me, A executes the S1 plan. On the contrary, if B does not give A the result he wants for some reason, Then A executes emergency plan S2. In this case, all potential risks are within the control of A
var resB = B(); var runA = function(){ resB.then(execS1,execS2); };
If A wants to complete something, it may not rely on a response from B, then the above code will change Cheng
var resB = B(); var resC = C(); ... var runA = function() { reqB .then(resC, execS2) .then(resD, execS3) .then(resE, execS4) ... .then(execS1); }; runA();
Here, every time an interrogator makes an unexpected response, a different processing mechanism is used. Although, the Promise specification does not require this, and even no processing is required, that is, (Do not pass in the second parameter of then), or handle it uniformly
A Promise may exist in three states: waiting( pending), Completed(fulfilled), Rejected(rejected)
The status of a Promise can only change from "waiting" "Go to "Complete" or "Reject", the reverse conversion is not allowed
Promise must implement the then method, (then is the core of promise), and then must return a Promise, the same promise's then can be called multiple times, and the callback execution order is the same as the order in which they are defined.
Thethen method accepts two parameters. The first parameter is the callback when success occurs, and the other is the callback when failure occurs. Then can accept another promise passed in, and also accepts a "class then" object or method, that is, thenable object
Please refer to this article JavaScript Promises of html5rocks. Currently, advanced browsers such as chrome and firefox have The Promise object has been built-in, providing more operation interfaces, such as Promise.all()
, which supports passing in an array of promises, and then is executed when all promises are completed. There is also a more friendly and powerful exception capture to deal with It should be enough for daily asynchronous programming.
Most of the popular js libraries today implement Promise to varying degrees, such as dojo, jQuery, Zepto, when.js, Q, etc., but they are only exposed Most of what comes out are Deferred
objects.
We see that no matter how complicated the Promise implementation is, its usage is very simple and the organized code is very clear. From now on, there is no need to I'm tortured by callbacks.
Finally, Promise is so elegant! But Promise only solves the problem of deep nesting of callbacks. Generator is what really simplifies JavaScript asynchronous programming. On the Node.js side, it is recommended to consider Generator.
Recommended tutorial: "JS Tutorial"
The above is the detailed content of Learning about Promise in ES6. For more information, please follow other related articles on the PHP Chinese website!