- Mastering JavaScript Promises
- Muzzamil Hussain
- 239字
- 2021-07-16 13:46:50
Standard behaviors of the Promise API
There are few standards as per a promise/proposal, which has to be fulfilled for the true implementation of the concept. These standards are the keys to implement promises, and any library/language must comply with it for true implementation.
A promise does the following:
- A promise returns an eventual value when a single completion of an operation occurs.
- A promise has three states: unfulfilled (when a promise is waiting to be processed), fulfilled (when a promise has been completed and the desired result has been obtained), and finally, failed (when the result of a promise was obtained but not as anticipated).
- Promise has a
then
property, which must be a function and must return a promise. In order to complete a promise,fulfilledHandler
,errorHandler
, andprogressHandler
must be called in. - With a promise, callback handlers return the fulfillment value from the returned promise.
- The promise value must be persistent. This should maintain a state, and within that state, the value must be preserved.
This API does not define how promises are created. It only provides a necessary interface that promise provides to promise consumers to interact with it. Implementations are free to define how promises are generated. Some promise may provide their own function to fulfill the promise and other promises may be fulfilled by mechanisms that are not visible to the promise consumer. Promises themselves may include other additional convenient methods as well.
推薦閱讀
- 企業級Java EE架構設計精深實踐
- Visual Basic程序開發(學習筆記)
- PHP基礎案例教程
- Building Mapping Applications with QGIS
- 程序員修煉之道:通向務實的最高境界(第2版)
- C程序設計實踐教程
- Visual Basic程序設計上機實驗教程
- JavaScript腳本特效編程給力起飛
- Python入門很輕松(微課超值版)
- 寫給程序員的Python教程
- Java EE 7 with GlassFish 4 Application Server
- Julia High Performance(Second Edition)
- Learning Grunt
- Java程序設計實用教程(第2版)
- 深入理解Kafka:核心設計與實踐原理