Skip to content

Commit e605593

Browse files
committed
Improve promises section
1 parent a8e2fbd commit e605593

File tree

1 file changed

+87
-5
lines changed
  • apps/components_guide_web/lib/components_guide_web/templates/web_standards

1 file changed

+87
-5
lines changed

apps/components_guide_web/lib/components_guide_web/templates/web_standards/promise.html.md

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ promisedValue.then(console.log);
2626

2727
In both cases, we will see it logged only once. This is because promises are run once are created eagerly.
2828

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.
3030

3131
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.
3232

@@ -61,15 +61,17 @@ const promisedResponse = fetch('https://swapi.dev/api/people/1/');
6161
We can chain the response to decode the JSON body of the response.
6262

6363
```javascript
64-
const promisedData = fetch('https://swapi.dev/api/people/1/').then(response => response.json());
64+
const promisedData = fetch('https://swapi.dev/api/people/1/')
65+
.then(response => response.json());
6566
```
6667

6768
What happens if we want to use this data again?
6869

6970
```javascript
7071
const promisedData = fetch('https://swapi.dev/api/people/1/')
7172
.then(response => {
72-
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');
7375
return response.json();
7476
});
7577

@@ -82,11 +84,11 @@ promisedData.then(data => {
8284
});
8385
```
8486

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.
8688

8789
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).
8890

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()`.
9092

9193
```javascript
9294
const promisedResponse = fetch('https://swapi.dev/api/people/1/');
@@ -113,3 +115,83 @@ fetch('https://swapi.dev/api/people/1/')
113115
### Async Await
114116

115117
The same applies if the code is rewritten to use `async await`. The underlying objects are still Promises.
118+
119+
The difference is that `async` requires a function, which means the code is run from scratch each time.
120+
121+
Here’s our API call written as a function using Promises:
122+
123+
```javascript
124+
const apiURL = new URL('https://swapi.dev/api/');
125+
126+
function fetchPerson(id) {
127+
return fetch(new URL(`people/${id}/`, apiURL))
128+
.then(response => {
129+
// How many times will we see this logged?
130+
console.log('decoding data');
131+
return response.json();
132+
});
133+
}
134+
135+
function main() {
136+
fetchPerson('1');
137+
fetchPerson('1');
138+
// We will see 'decoding data' twice now, as our function is run from scratch twice.
139+
}
140+
141+
main();
142+
```
143+
144+
```javascript
145+
const apiURL = new URL('https://swapi.dev/api/');
146+
147+
async function fetchPerson(id) {
148+
const response = await fetch(new URL(`people/${id}/`, apiURL));
149+
console.log('decoding data');
150+
return response.json(); // Note: if we return a Promise, then there’s no need to await
151+
}
152+
153+
async function main() {
154+
await fetchPerson('1');
155+
await fetchPerson('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+
async function main() {
166+
const promise = 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+
function main() {
180+
const promise = 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.
196+
197+

0 commit comments

Comments
 (0)