(1) Promise meaning
Summary:
Promise is asynchronous programming a solution than traditional solutions - more rational and more powerful - callback functions and events. It was first proposed and implemented by the community, ES6 be written into the standard language, unified usage, native in the Promise object.
The so-calledPromise
, it simply is a container, which holds the event will end sometime in the future (usually an asynchronous operation) results. Syntactically, Promise is an object, you can get messages from the asynchronous operation of it.
Promise to provide a unified API, a variety of asynchronous operations can be handled the same way.
Features:
Promise object has the following two characteristics: 1 , the state is not subject to outside influence. 2 , once the state change, will not change, at any time this result can be obtained. As long as these two things happens on the solidification state, it will not be changed and will keep the result,
then called resolved (finalized). If the change has occurred, you add a callback function to Promise object will immediately get this result.
Advantages and disadvantages:
Advantages: With Promise object, the process may be asynchronous operation to synchronous operation expressed, to avoid deeply nested callback function. Further, Promise object provides a unified interface, so that the control of asynchronous operations easier.
Drawback: Promise also has some disadvantages. First of all, you can not cancel the Promise, it will be executed immediately once the new can not be canceled midway. Secondly, if you do not set a callback function, internal Promise thrown error, it does not react to the outside.
Third, when in pending status, progress is currently no way of knowing where a stage (beginning or nearing completion).
(2) pay attention to grammar
In general, do not define Reject state in which then callback method (that is, then the second argument), recommended to always use the catch method . As follows
The second writing is better than a first writing, on the grounds that the second writing can be performed in front of the error capture method then, are closer to synchronous writing (the try / the catch) .
Therefore, we recommend always using the catch method, instead of using the method then the second parameter.
(3) written recommendations
Generally always recommended, Promise objects keep catch behind the method, which can handle errors that occur inside the Promise.
method returns a catch or Promise objects, then call back method may also be followed.
As follows:
After running the above code catch
method specified callback function will then run behind the then
method specified callback function. If there is no error, it will skip catch
methods.
(4) catch
Among the methods, can then throw an error
(6)Promise.prototype.then()
(7)Promise.prototype.catch()
(8) Promise.prototype.finally () --- ES2018 new feature that is ES9
(9)Promise.all()
Case are as follows:
(10) Promise.race () --- race, horse racing
Case are as follows: a file size is 3080KB, b file size is 1kb
The results are as follows: prints out the BBB, since a file is too large, slow reading, b document reading is completed immediately change state
(11) Application
.