You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: manuscript/10-Promises.md
+42-42Lines changed: 42 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -337,7 +337,7 @@ I> If you pass a promise to either `Promise.resolve()` or `Promise.reject()`, th
337
337
Both `Promise.resolve()` and `Promise.reject()` also accept non-promise thenables as arguments and will create a new promise that is called after `then()`. A non-promise thenable is created when a object has a `then()` method that accepts two arguments: `resolve` and `reject`. For example:
338
338
339
339
```js
340
-
var thenable = {
340
+
let thenable = {
341
341
then:function(resolve, reject) {
342
342
resolve(42);
343
343
}
@@ -347,13 +347,13 @@ var thenable = {
347
347
The `thenable` object in this example has no characteristics associated with a promise other than the `then()` method. It can be converted into a fulfilled promise using `Promise.resolve()`:
348
348
349
349
```js
350
-
var thenable = {
350
+
let thenable = {
351
351
then:function(resolve, reject) {
352
352
resolve(42);
353
353
}
354
354
};
355
355
356
-
var p1 =Promise.resolve(thenable);
356
+
let p1 =Promise.resolve(thenable);
357
357
p1.then(function(value) {
358
358
console.log(value); // 42
359
359
});
@@ -362,13 +362,13 @@ p1.then(function(value) {
362
362
In this example, `Promise.resolve()` calls `thenable.then()` so that a promise state can be determined. Since this code calls `resolve(42)`, the promise state for `thenable` is fulfilled. A new promise is created in the fulfilled state with the value passed from the thenable (42) so the fulfillment handler for `p1` receives 42 as the value. The same process can be used with `Promise.reject()` in order to create a rejected promise from a thenable:
363
363
364
364
```js
365
-
var thenable = {
365
+
let thenable = {
366
366
then:function(resolve, reject) {
367
367
reject(42);
368
368
}
369
369
};
370
370
371
-
var p1 =Promise.reject(thenable);
371
+
let p1 =Promise.reject(thenable);
372
372
p1.catch(function(value) {
373
373
console.log(value); // 42
374
374
});
@@ -548,11 +548,11 @@ In this version of the code, the second `console.log(value)` is never executed b
548
548
Returning primitive values from fulfillment and rejection handlers allows passing of data between promises, but what if you return an object? If the object is a promise, then there's an extra step that's taken to determine how to proceed. Consider the following example:
549
549
550
550
```js
551
-
var p1 =newPromise(function(resolve, reject) {
551
+
let p1 =newPromise(function(resolve, reject) {
552
552
resolve(42);
553
553
});
554
554
555
-
var p2 =newPromise(function(resolve, reject) {
555
+
let p2 =newPromise(function(resolve, reject) {
556
556
resolve(43);
557
557
});
558
558
@@ -569,15 +569,15 @@ In this code, `p1` schedules a job that resolves to 42. In the fulfillment handl
569
569
The important thing to recognize about this pattern is that the second fulfillment handler is not added to `p2`, but rather to a third promise. It's this third promise that the second fulfillment handler is attached to. The previous example is equivalent to this:
570
570
571
571
```js
572
-
var p1 =newPromise(function(resolve, reject) {
572
+
let p1 =newPromise(function(resolve, reject) {
573
573
resolve(42);
574
574
});
575
575
576
-
var p2 =newPromise(function(resolve, reject) {
576
+
let p2 =newPromise(function(resolve, reject) {
577
577
resolve(43);
578
578
});
579
579
580
-
var p3 =p1.then(function(value) {
580
+
let p3 =p1.then(function(value) {
581
581
console.log(value); // 42
582
582
return p2;
583
583
});
@@ -590,11 +590,11 @@ p3.then(function(value) {
590
590
Here, it's clear that the second fulfillment handler is attached to `p3` rather than `p2`. This is a subtle but important distinction as the second fulfillment handler will not be called if `p2` is rejected. For example:
591
591
592
592
```js
593
-
var p1 =newPromise(function(resolve, reject) {
593
+
let p1 =newPromise(function(resolve, reject) {
594
594
resolve(42);
595
595
});
596
596
597
-
var p2 =newPromise(function(resolve, reject) {
597
+
let p2 =newPromise(function(resolve, reject) {
598
598
reject(43);
599
599
});
600
600
@@ -609,11 +609,11 @@ p1.then(function(value) {
609
609
In this example, the second fulfillment handler is never called because `p2` is rejected. You could, however, attach a rejection handler instead:
610
610
611
611
```js
612
-
var p1 =newPromise(function(resolve, reject) {
612
+
let p1 =newPromise(function(resolve, reject) {
613
613
resolve(42);
614
614
});
615
615
616
-
var p2 =newPromise(function(resolve, reject) {
616
+
let p2 =newPromise(function(resolve, reject) {
617
617
reject(43);
618
618
});
619
619
@@ -630,15 +630,15 @@ Here, the rejection handler is called as a result of `p2` being rejected. The re
630
630
Returning thenables from fulfillment or rejection handlers doesn't change when the promise executors are executed. The first defined promise will run its executor first, followed by the second, and so on. Returning thenables simply allows you to define additional responses. You defer the execution of fulfillment handlers by creating a new promise within a fulfillment handler. For example:
631
631
632
632
```js
633
-
var p1 =newPromise(function(resolve, reject) {
633
+
let p1 =newPromise(function(resolve, reject) {
634
634
resolve(42);
635
635
});
636
636
637
637
p1.then(function(value) {
638
638
console.log(value); // 42
639
639
640
640
// create a new promise
641
-
var p2 =newPromise(function(resolve, reject) {
641
+
let p2 =newPromise(function(resolve, reject) {
642
642
resolve(43);
643
643
});
644
644
@@ -659,19 +659,19 @@ Up to this point, each example has dealt with responding to one promise at a tim
659
659
The `Promise.all()` method accepts a single argument, which is an iterable of promises to monitor, and returns a promise that is resolved only when every promise in the iterable is resolved. The returned promise is fulfilled when every promise in the iterable is fulfilled, for example:
660
660
661
661
```js
662
-
var p1 =newPromise(function(resolve, reject) {
662
+
let p1 =newPromise(function(resolve, reject) {
663
663
resolve(42);
664
664
});
665
665
666
-
var p2 =newPromise(function(resolve, reject) {
666
+
let p2 =newPromise(function(resolve, reject) {
667
667
resolve(43);
668
668
});
669
669
670
-
var p3 =newPromise(function(resolve, reject) {
670
+
let p3 =newPromise(function(resolve, reject) {
671
671
resolve(44);
672
672
});
673
673
674
-
var p4 =Promise.all([p1, p2, p3]);
674
+
let p4 =Promise.all([p1, p2, p3]);
675
675
676
676
p4.then(function(value) {
677
677
console.log(value); // [42, 43, 44]
@@ -683,19 +683,19 @@ Each of the promises in this example resolves with a number. The call to `Promis
683
683
If any of the promises passed to `Promise.all()` is rejected, the returned promise is immediately rejected without waiting for the other promises to complete:
684
684
685
685
```js
686
-
var p1 =newPromise(function(resolve, reject) {
686
+
let p1 =newPromise(function(resolve, reject) {
687
687
resolve(42);
688
688
});
689
689
690
-
var p2 =newPromise(function(resolve, reject) {
690
+
let p2 =newPromise(function(resolve, reject) {
691
691
reject(43);
692
692
});
693
693
694
-
var p3 =newPromise(function(resolve, reject) {
694
+
let p3 =newPromise(function(resolve, reject) {
695
695
resolve(44);
696
696
});
697
697
698
-
var p4 =Promise.all([p1, p2, p3]);
698
+
let p4 =Promise.all([p1, p2, p3]);
699
699
700
700
p4.catch(function(value) {
701
701
console.log(value); // 43
@@ -709,17 +709,17 @@ In this example, `p2` is rejected with a value of 43. The rejection handler for
709
709
The `Promise.race()` method provides a slightly different take on monitoring multiple promises. This method also accepts an iterable of promises to monitor and returns a promise, however, the returned promise is settled as soon as the first promise is settled. So instead of waiting for all promises to be fulfilled, as in `Promise.all()`, the returned promise is fulfilled as soon as any of the promises is fulfilled. For example:
710
710
711
711
```js
712
-
var p1 =Promise.resolve(42);
712
+
let p1 =Promise.resolve(42);
713
713
714
-
var p2 =newPromise(function(resolve, reject) {
714
+
let p2 =newPromise(function(resolve, reject) {
715
715
resolve(43);
716
716
});
717
717
718
-
var p3 =newPromise(function(resolve, reject) {
718
+
let p3 =newPromise(function(resolve, reject) {
719
719
resolve(44);
720
720
});
721
721
722
-
var p4 =Promise.race([p1, p2, p3]);
722
+
let p4 =Promise.race([p1, p2, p3]);
723
723
724
724
p4.then(function(value) {
725
725
console.log(value); // 42
@@ -729,17 +729,17 @@ p4.then(function(value) {
729
729
In this code, `p1` is created as a fulfilled promise while the others schedule jobs. The fulfillment handler for `p4` is then called with the value of 42 and ignores the other promises completely. The promises passed to `Promise.race()` are truly in a race to see which is settled first. If the first promise to settle is fulfilled, then the returned promise is fulfilled; if the first promise to settle is rejected, then the returned promise is rejected. Here's an example with a rejection:
730
730
731
731
```js
732
-
var p1 =newPromise(function(resolve, reject) {
732
+
let p1 =newPromise(function(resolve, reject) {
733
733
resolve(42);
734
734
});
735
735
736
-
var p2 =Promise.reject(43);
736
+
let p2 =Promise.reject(43);
737
737
738
-
var p3 =newPromise(function(resolve, reject) {
738
+
let p3 =newPromise(function(resolve, reject) {
739
739
resolve(44);
740
740
});
741
741
742
-
var p4 =Promise.race([p1, p2, p3]);
742
+
let p4 =Promise.race([p1, p2, p3]);
743
743
744
744
p4.catch(function(value) {
745
745
console.log(value); // 43
@@ -753,9 +753,9 @@ Here, `p4` is rejected because `p2` is already in the rejected state when `Promi
753
753
Back in chapter 8, you learned about generators and how they can be used for asynchronous task scheduling such as the following:
The pain point of this implementation was needing to keep track of `task` and calling the appropriate methods on it in every single asynchronous function you use (such as `readConfigFile()`). With promises, you can greatly simplify and generalize this process by ensuring that each asynchronous operation returns a promise. That common interface means you can greatly simplify asynchronous code:
781
781
782
782
```js
783
-
var fs =require("fs");
783
+
let fs =require("fs");
784
784
785
785
functionrun(taskDef) {
786
786
787
787
// create the iterator
788
-
var task =taskDef();
788
+
let task =taskDef();
789
789
790
790
// start the task
791
791
task.next();
792
792
793
793
// recursive function to iterate through
794
794
(functionstep() {
795
795
796
-
var result =task.next(),
796
+
let result =task.next(),
797
797
promise;
798
798
799
799
// if there's more to do
@@ -825,7 +825,7 @@ function readConfigFile() {
825
825
}
826
826
827
827
run(function*() {
828
-
var contents =yieldreadConfigFile();
828
+
let contents =yieldreadConfigFile();
829
829
doSomethingWith(contents);
830
830
console.log("Done");
831
831
});
@@ -854,7 +854,7 @@ class MyPromise extends Promise {
854
854
855
855
}
856
856
857
-
var promise =newMyPromise(function(resolve, reject) {
857
+
let promise =newMyPromise(function(resolve, reject) {
858
858
resolve(42);
859
859
});
860
860
@@ -872,11 +872,11 @@ Since static methods are also inherited, that means `MyPromise.resolve()`, `MyPr
872
872
Both `MyPromise.resolve()` and `MyPromise.reject()` will return an instance of `MyPromise` regardless of the value passed. So if a built-in promise is passed to either, it will be resolved or rejected and a new `MyPromise` so you can assign fulfillment and rejection handlers. For example:
0 commit comments