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
In both cases, we will see it logged only once. This is because promises are run once are created eagerly.
28
28
29
-
Listening to a promise using `.then()` does not affect nor start that promise. It has no side-effects on the source promise.
29
+
Listening to a promise using `.then()` does not affect nor start that promise. It has no side-effect on the source promise.
30
30
31
31
Once a promise has been created, then you may wait to hear its result one time, fifteen times, or not at all, and the original promise will behave the same.
console.log('decoding data'); // How many times will we see this logged?
73
+
// How many times will we see this logged?
74
+
console.log('decoding data');
73
75
returnresponse.json();
74
76
});
75
77
@@ -82,11 +84,11 @@ promisedData.then(data => {
82
84
});
83
85
```
84
86
85
-
Here will see `decoding data` logged once. The `fetch()` call returns a Promise, which is chained using `.then()`to unwrap the underlying JSON body by calling `.json()` on the response.
87
+
Here we will see *‘decoding data’* logged once. The `fetch()` call returns a Promise, which is chained using `.then()`where we decode the underlying JSON body by calling `.json()` on the response.
86
88
87
89
We can continue to think of these as eventual values. Once these values have been cast, they cannot change (technically we could mutate anything as JavaScript gives us free reign but we shouldn’t).
88
90
89
-
The response from `fetch()` is one eventual value. The decode JSON is another eventual value, and actually has two Promises, one created by the `.json()` method, and another wrapping that created by `.then()`.
91
+
The response from `fetch()` is one eventual value. The decoded JSON is another eventual value, and actually has two Promises, one created by the `.json()` method, and another wrapping that which was created by `.then()`.
returnresponse.json(); // Note: if we return a Promise, then there’s no need to await
151
+
}
152
+
153
+
asyncfunctionmain() {
154
+
awaitfetchPerson('1');
155
+
awaitfetchPerson('1');
156
+
// We will see 'decoding data' twice now, as our function is run from scratch twice.
157
+
}
158
+
159
+
main();
160
+
```
161
+
162
+
However, if we use the result from our `fetchPerson()` function (which we be a Promise), and `await` that twice (or more) then we are running the function only once.
163
+
164
+
```javascript
165
+
asyncfunctionmain() {
166
+
constpromise=fetchPerson('1');
167
+
168
+
await promise;
169
+
await promise;
170
+
// We will see 'decoding data' only once, as our function is run only once.
171
+
}
172
+
173
+
main();
174
+
```
175
+
176
+
This is conceptually similar to calling `.then()` on the promise twice.
177
+
178
+
```javascript
179
+
functionmain() {
180
+
constpromise=fetchPerson('1');
181
+
182
+
promise.then(data=> {
183
+
// Do nothing
184
+
});
185
+
promise.then(data=> {
186
+
// Do nothing
187
+
});
188
+
189
+
// We will see 'decoding data' only once, as our function is run only once.
190
+
}
191
+
192
+
main();
193
+
```
194
+
195
+
As we learned earlier, listening to a promise using `.then()` does not affect nor start that promise — it has no side-effect on that promise. And `await` behaves the same — it also has no side-effect on the source promise. It simply waits for its eventual value.
0 commit comments