- 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.
推薦閱讀
- 程序員修煉之道:程序設計入門30講
- Learning Apex Programming
- RTC程序設計:實時音視頻權威指南
- Visual C++數字圖像處理技術詳解
- Apache Kafka Quick Start Guide
- NGINX Cookbook
- Python:Deeper Insights into Machine Learning
- Mobile Forensics:Advanced Investigative Strategies
- JavaScript Concurrency
- C++服務器開發精髓
- PHP動態網站開發實踐教程
- Wearable:Tech Projects with the Raspberry Pi Zero
- 算法訓練營:海量圖解+競賽刷題(入門篇)
- Web前端開發全程實戰:HTML5+CSS3+JavaScript+jQuery+Bootstrap
- 零基礎學西門子PLC編程:入門、提高、應用、實例