Skip to content

Commit d13adf2

Browse files
committed
let all the things in Promises chapter
1 parent 248eaeb commit d13adf2

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

manuscript/10-Promises.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Because JavaScript engines can only execute one piece of code at a time, it's ne
1919
When a user clicks a button or presses key on the keyboard, an *event* is triggered (such as `onclick`). That event may be used to respond to the interaction by adding a new job to the back of the job queue. This is the most basic form of asynchronous programming JavaScript has: the event handler code doesn't execute until the event fires, and when it does execute, it has the appropriate context. For example:
2020

2121
```js
22-
var button = document.getElementById("my-btn");
22+
let button = document.getElementById("my-btn");
2323
button.onclick = function(event) {
2424
console.log("Clicked");
2525
};
@@ -117,7 +117,7 @@ A promise is a placeholder for the result of an asynchronous operation. Instead
117117

118118
```js
119119
// readFile promises to complete at some point in the future
120-
var promise = readFile("example.txt");
120+
let promise = readFile("example.txt");
121121
```
122122

123123
In this code, `readFile()` doesn't actually start reading the file immediately (that will happen later). It returns a promise object that represents the asynchronous operation so you can work with it later.
@@ -138,7 +138,7 @@ The `then()` method is present on all promises and takes two arguments (any obje
138138
Both arguments are optional, so you can listen for any combination of fulfillment and rejection. For example:
139139

140140
```js
141-
var promise = readFile("example.txt");
141+
let promise = readFile("example.txt");
142142

143143
// listen for both fulfillment and rejection
144144
promise.then(function(contents) {
@@ -187,7 +187,7 @@ New promises are created through the `Promise` constructor. This constructor acc
187187
```js
188188
// Node.js example
189189

190-
var fs = require("fs");
190+
let fs = require("fs");
191191

192192
function readFile(filename) {
193193
return new Promise(function(resolve, reject) {
@@ -208,7 +208,7 @@ function readFile(filename) {
208208
});
209209
}
210210

211-
var promise = readFile("example.txt");
211+
let promise = readFile("example.txt");
212212

213213
// listen for both fulfillment and rejection
214214
promise.then(function(contents) {
@@ -246,7 +246,7 @@ You can tell from the output that the function passed to `setTimeout()` was exec
246246
The promise executor is added to the job queue immediately, meaning it will execute only after all previous jobs are complete. For example:
247247

248248
```js
249-
var promise = new Promise(function(resolve, reject) {
249+
let promise = new Promise(function(resolve, reject) {
250250
console.log("Promise");
251251
resolve();
252252
});
@@ -264,7 +264,7 @@ Promise
264264
The takeaway is that the executor doesn't run until sometime after the current job has finished executing. The same is true for the functions passed to `then()` and `catch()`, as these will also be added to the job queue, but only after the executor job. Here's an example:
265265

266266
```js
267-
var promise = new Promise(function(resolve, reject) {
267+
let promise = new Promise(function(resolve, reject) {
268268
console.log("Promise");
269269
resolve();
270270
});

0 commit comments

Comments
 (0)