- Mastering JavaScript Promises
- Muzzamil Hussain
- 249字
- 2021-07-16 13:46:50
The states and return values of a promise
From Chapter 1, Promises.js, we are already aware that a promise is based on three states. Let's brush up our memory on these states, in accordance with promises paradigm.
Promise has three states:
- Unfulfilled promise
- Fulfilled promise
- Failed promise
A promise exists within these three states.
The beginning of a promise is from an unfulfilled state. This is due to the fact that a promise is a proxy for an unknown value.
When the promise is filled with the value it's waiting for, it's in the fulfilled state. The promise will be labeled as failed if it returns an exception.
A promise may move from an unfulfilled to a fulfilled or failed state. Observers (or the objects/events waiting) are notified when the promise is either rejected or fulfilled. Once the promise is rejected or resolved, its output (value or state) cannot be modified.
The following code snippet will help you understand more easily than theory:
// Promise to be filled with future value var futureValue = new Promise(); // .then() will return a new promise var anotherFutureValue = futureValue.then(); // Promise state handlers (must be a function ). // The returned value of the fulfilled / failed handler will be the value of the promise. futureValue.then({ // Called if/when the promise is fulfilled fulfilledHandler: function() {}, // Called if/when the promise fails errorHandler: function() {}, // Called for progress events (not all implementations of promises have this) progressHandler: function() {} });
- Java系統(tǒng)分析與架構(gòu)設(shè)計(jì)
- Machine Learning with R Cookbook(Second Edition)
- 深入實(shí)踐Spring Boot
- Python GUI Programming Cookbook
- Building Mapping Applications with QGIS
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- 深入RabbitMQ
- .NET 3.5編程
- 智能手機(jī)APP UI設(shè)計(jì)與應(yīng)用任務(wù)教程
- Unity 3D/2D移動(dòng)開(kāi)發(fā)實(shí)戰(zhàn)教程
- C++ Application Development with Code:Blocks
- Zabbix Performance Tuning
- 計(jì)算機(jī)應(yīng)用技能實(shí)訓(xùn)教程
- App Inventor 2 Essentials
- HTML5游戲開(kāi)發(fā)實(shí)戰(zhàn)