Skip to content

Commit a3f0162

Browse files
committed
More info about promise lifecycle
1 parent dff7185 commit a3f0162

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

manuscript/10-Promises.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ The intent is to use a combination of `then()` and `catch()` to properly handle
182182

183183
W> If you don't attach a rejection handler to a promise, all failures happen silently. It's a good idea to always attach a rejection handler even if it just logs the failure.
184184

185+
One of the unique aspects of promises is that a fulfillment or rejection handler will still be executed if it is added after the promise is already settled. This allows you to add new fulfillment and rejection handlers at any point in time and be assured that they will be called. For example:
186+
187+
```js
188+
let promise = readFile("example.txt");
189+
190+
// original fulfillment handler
191+
promise.then(function(contents) {
192+
console.log(contents);
193+
194+
// now add another
195+
promise.then(function(contents) {
196+
console.log(contents);
197+
});
198+
});
199+
```
200+
201+
In this example, the fulfillment handler adds another fulfillment handler to the same promise.The promise is already fulfilled at this point, so the new fulfillment handler is added to the job queue and called when ready. Rejection handlers work the same way in that they can be added at any point and are guaranteed to be called.
202+
185203
### Creating Promises
186204

187205
New promises are created through the `Promise` constructor. This constructor accepts a single argument, which is a function (called the *executor*) containing the code to execute when the promise is added to the job queue. The executor is passed two functions as arguments, `resolve()` and `reject()`. The `resolve()` function is called when the executor has finished successfully in order to signal that the promise is ready to be resolved while the `reject()` function indicates that the executor has failed. Here's an example using a promise in Node.js to implement the `readFile()` function from earlier in this chapter:

0 commit comments

Comments
 (0)