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
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ Because JavaScript engines can only execute one piece of code at a time, it's ne
19
19
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:
20
20
21
21
```js
22
-
var button =document.getElementById("my-btn");
22
+
let button =document.getElementById("my-btn");
23
23
button.onclick=function(event) {
24
24
console.log("Clicked");
25
25
};
@@ -117,7 +117,7 @@ A promise is a placeholder for the result of an asynchronous operation. Instead
117
117
118
118
```js
119
119
// readFile promises to complete at some point in the future
120
-
var promise =readFile("example.txt");
120
+
let promise =readFile("example.txt");
121
121
```
122
122
123
123
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
138
138
Both arguments are optional, so you can listen for any combination of fulfillment and rejection. For example:
139
139
140
140
```js
141
-
var promise =readFile("example.txt");
141
+
let promise =readFile("example.txt");
142
142
143
143
// listen for both fulfillment and rejection
144
144
promise.then(function(contents) {
@@ -187,7 +187,7 @@ New promises are created through the `Promise` constructor. This constructor acc
187
187
```js
188
188
// Node.js example
189
189
190
-
var fs =require("fs");
190
+
let fs =require("fs");
191
191
192
192
functionreadFile(filename) {
193
193
returnnewPromise(function(resolve, reject) {
@@ -208,7 +208,7 @@ function readFile(filename) {
208
208
});
209
209
}
210
210
211
-
var promise =readFile("example.txt");
211
+
let promise =readFile("example.txt");
212
212
213
213
// listen for both fulfillment and rejection
214
214
promise.then(function(contents) {
@@ -246,7 +246,7 @@ You can tell from the output that the function passed to `setTimeout()` was exec
246
246
The promise executor is added to the job queue immediately, meaning it will execute only after all previous jobs are complete. For example:
247
247
248
248
```js
249
-
var promise =newPromise(function(resolve, reject) {
249
+
let promise =newPromise(function(resolve, reject) {
250
250
console.log("Promise");
251
251
resolve();
252
252
});
@@ -264,7 +264,7 @@ Promise
264
264
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:
265
265
266
266
```js
267
-
var promise =newPromise(function(resolve, reject) {
267
+
let promise =newPromise(function(resolve, reject) {
0 commit comments