Skip to content

Commit 03ff03b

Browse files
committed
Switch to using let
1 parent 72db073 commit 03ff03b

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

manuscript/10-Promises.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ I> If you pass a promise to either `Promise.resolve()` or `Promise.reject()`, th
337337
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:
338338

339339
```js
340-
var thenable = {
340+
let thenable = {
341341
then: function(resolve, reject) {
342342
resolve(42);
343343
}
@@ -347,13 +347,13 @@ var thenable = {
347347
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()`:
348348

349349
```js
350-
var thenable = {
350+
let thenable = {
351351
then: function(resolve, reject) {
352352
resolve(42);
353353
}
354354
};
355355

356-
var p1 = Promise.resolve(thenable);
356+
let p1 = Promise.resolve(thenable);
357357
p1.then(function(value) {
358358
console.log(value); // 42
359359
});
@@ -362,13 +362,13 @@ p1.then(function(value) {
362362
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:
363363

364364
```js
365-
var thenable = {
365+
let thenable = {
366366
then: function(resolve, reject) {
367367
reject(42);
368368
}
369369
};
370370

371-
var p1 = Promise.reject(thenable);
371+
let p1 = Promise.reject(thenable);
372372
p1.catch(function(value) {
373373
console.log(value); // 42
374374
});
@@ -548,11 +548,11 @@ In this version of the code, the second `console.log(value)` is never executed b
548548
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:
549549

550550
```js
551-
var p1 = new Promise(function(resolve, reject) {
551+
let p1 = new Promise(function(resolve, reject) {
552552
resolve(42);
553553
});
554554

555-
var p2 = new Promise(function(resolve, reject) {
555+
let p2 = new Promise(function(resolve, reject) {
556556
resolve(43);
557557
});
558558

@@ -569,15 +569,15 @@ In this code, `p1` schedules a job that resolves to 42. In the fulfillment handl
569569
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:
570570

571571
```js
572-
var p1 = new Promise(function(resolve, reject) {
572+
let p1 = new Promise(function(resolve, reject) {
573573
resolve(42);
574574
});
575575

576-
var p2 = new Promise(function(resolve, reject) {
576+
let p2 = new Promise(function(resolve, reject) {
577577
resolve(43);
578578
});
579579

580-
var p3 = p1.then(function(value) {
580+
let p3 = p1.then(function(value) {
581581
console.log(value); // 42
582582
return p2;
583583
});
@@ -590,11 +590,11 @@ p3.then(function(value) {
590590
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:
591591

592592
```js
593-
var p1 = new Promise(function(resolve, reject) {
593+
let p1 = new Promise(function(resolve, reject) {
594594
resolve(42);
595595
});
596596

597-
var p2 = new Promise(function(resolve, reject) {
597+
let p2 = new Promise(function(resolve, reject) {
598598
reject(43);
599599
});
600600

@@ -609,11 +609,11 @@ p1.then(function(value) {
609609
In this example, the second fulfillment handler is never called because `p2` is rejected. You could, however, attach a rejection handler instead:
610610

611611
```js
612-
var p1 = new Promise(function(resolve, reject) {
612+
let p1 = new Promise(function(resolve, reject) {
613613
resolve(42);
614614
});
615615

616-
var p2 = new Promise(function(resolve, reject) {
616+
let p2 = new Promise(function(resolve, reject) {
617617
reject(43);
618618
});
619619

@@ -630,15 +630,15 @@ Here, the rejection handler is called as a result of `p2` being rejected. The re
630630
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:
631631

632632
```js
633-
var p1 = new Promise(function(resolve, reject) {
633+
let p1 = new Promise(function(resolve, reject) {
634634
resolve(42);
635635
});
636636

637637
p1.then(function(value) {
638638
console.log(value); // 42
639639

640640
// create a new promise
641-
var p2 = new Promise(function(resolve, reject) {
641+
let p2 = new Promise(function(resolve, reject) {
642642
resolve(43);
643643
});
644644

@@ -659,19 +659,19 @@ Up to this point, each example has dealt with responding to one promise at a tim
659659
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:
660660

661661
```js
662-
var p1 = new Promise(function(resolve, reject) {
662+
let p1 = new Promise(function(resolve, reject) {
663663
resolve(42);
664664
});
665665

666-
var p2 = new Promise(function(resolve, reject) {
666+
let p2 = new Promise(function(resolve, reject) {
667667
resolve(43);
668668
});
669669

670-
var p3 = new Promise(function(resolve, reject) {
670+
let p3 = new Promise(function(resolve, reject) {
671671
resolve(44);
672672
});
673673

674-
var p4 = Promise.all([p1, p2, p3]);
674+
let p4 = Promise.all([p1, p2, p3]);
675675

676676
p4.then(function(value) {
677677
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
683683
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:
684684

685685
```js
686-
var p1 = new Promise(function(resolve, reject) {
686+
let p1 = new Promise(function(resolve, reject) {
687687
resolve(42);
688688
});
689689

690-
var p2 = new Promise(function(resolve, reject) {
690+
let p2 = new Promise(function(resolve, reject) {
691691
reject(43);
692692
});
693693

694-
var p3 = new Promise(function(resolve, reject) {
694+
let p3 = new Promise(function(resolve, reject) {
695695
resolve(44);
696696
});
697697

698-
var p4 = Promise.all([p1, p2, p3]);
698+
let p4 = Promise.all([p1, p2, p3]);
699699

700700
p4.catch(function(value) {
701701
console.log(value); // 43
@@ -709,17 +709,17 @@ In this example, `p2` is rejected with a value of 43. The rejection handler for
709709
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:
710710

711711
```js
712-
var p1 = Promise.resolve(42);
712+
let p1 = Promise.resolve(42);
713713

714-
var p2 = new Promise(function(resolve, reject) {
714+
let p2 = new Promise(function(resolve, reject) {
715715
resolve(43);
716716
});
717717

718-
var p3 = new Promise(function(resolve, reject) {
718+
let p3 = new Promise(function(resolve, reject) {
719719
resolve(44);
720720
});
721721

722-
var p4 = Promise.race([p1, p2, p3]);
722+
let p4 = Promise.race([p1, p2, p3]);
723723

724724
p4.then(function(value) {
725725
console.log(value); // 42
@@ -729,17 +729,17 @@ p4.then(function(value) {
729729
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:
730730

731731
```js
732-
var p1 = new Promise(function(resolve, reject) {
732+
let p1 = new Promise(function(resolve, reject) {
733733
resolve(42);
734734
});
735735

736-
var p2 = Promise.reject(43);
736+
let p2 = Promise.reject(43);
737737

738-
var p3 = new Promise(function(resolve, reject) {
738+
let p3 = new Promise(function(resolve, reject) {
739739
resolve(44);
740740
});
741741

742-
var p4 = Promise.race([p1, p2, p3]);
742+
let p4 = Promise.race([p1, p2, p3]);
743743

744744
p4.catch(function(value) {
745745
console.log(value); // 43
@@ -753,9 +753,9 @@ Here, `p4` is rejected because `p2` is already in the rejected state when `Promi
753753
Back in chapter 8, you learned about generators and how they can be used for asynchronous task scheduling such as the following:
754754

755755
```js
756-
var fs = require("fs");
756+
let fs = require("fs");
757757

758-
var task;
758+
let task;
759759

760760
function readConfigFile() {
761761
fs.readFile("config.json", function(err, contents) {
@@ -768,7 +768,7 @@ function readConfigFile() {
768768
}
769769

770770
function *init() {
771-
var contents = yield readConfigFile();
771+
let contents = yield readConfigFile();
772772
doSomethingWith(contents);
773773
console.log("Done");
774774
}
@@ -780,20 +780,20 @@ task.next();
780780
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:
781781

782782
```js
783-
var fs = require("fs");
783+
let fs = require("fs");
784784

785785
function run(taskDef) {
786786

787787
// create the iterator
788-
var task = taskDef();
788+
let task = taskDef();
789789

790790
// start the task
791791
task.next();
792792

793793
// recursive function to iterate through
794794
(function step() {
795795

796-
var result = task.next(),
796+
let result = task.next(),
797797
promise;
798798

799799
// if there's more to do
@@ -825,7 +825,7 @@ function readConfigFile() {
825825
}
826826

827827
run(function *() {
828-
var contents = yield readConfigFile();
828+
let contents = yield readConfigFile();
829829
doSomethingWith(contents);
830830
console.log("Done");
831831
});
@@ -854,7 +854,7 @@ class MyPromise extends Promise {
854854

855855
}
856856

857-
var promise = new MyPromise(function(resolve, reject) {
857+
let promise = new MyPromise(function(resolve, reject) {
858858
resolve(42);
859859
});
860860

@@ -872,11 +872,11 @@ Since static methods are also inherited, that means `MyPromise.resolve()`, `MyPr
872872
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:
873873

874874
```js
875-
var p1 = new Promise(function(resolve, reject) {
875+
let p1 = new Promise(function(resolve, reject) {
876876
resolve(42);
877877
});
878878

879-
var p2 = MyPromise.resolve(p1);
879+
let p2 = MyPromise.resolve(p1);
880880
p2.success(function(value) {
881881
console.log(value); // 42
882882
});

0 commit comments

Comments
 (0)