Skip to content

Commit d7f985e

Browse files
committed
Improve readability of promise page
1 parent bb97960 commit d7f985e

File tree

1 file changed

+20
-14
lines changed
  • apps/components_guide_web/lib/components_guide_web/templates/web_standards

1 file changed

+20
-14
lines changed

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ Let's compare two code samples. How many times will we see _Creating value_ logg
88

99
```js
1010
const promisedValue = new Promise((resolve, reject) => {
11-
console.log("Creating value"); // Will this be logged once or not at all?
11+
// Will this be logged once or not at all?
12+
console.log("Creating value");
1213
resolve(40 + 2);
1314
});
1415
```
1516

1617
```js
1718
const promisedValue = new Promise((resolve, reject) => {
18-
console.log("Creating value"); // Will this logged three times or once?
19+
// Will this logged three times or once?
20+
console.log("Creating value");
1921
resolve(40 + 2);
2022
});
2123

@@ -133,9 +135,10 @@ function fetchPerson(id) {
133135
}
134136

135137
function main() {
136-
fetchPerson('1');
137-
fetchPerson('1');
138-
// We will see 'decoding data' twice now, as our function is run from scratch twice.
138+
fetchPerson('1'); // *Creates* promise
139+
fetchPerson('1'); // *Creates* another promise
140+
// We will see 'decoding data' logged twice,
141+
// as our function is run from scratch twice.
139142
}
140143

141144
main();
@@ -148,14 +151,17 @@ const apiURL = new URL('https://swapi.dev/api/');
148151

149152
async function fetchPerson(id) {
150153
const response = await fetch(new URL(`people/${id}/`, apiURL));
154+
// How many times will we see this logged?
151155
console.log('decoding data');
152-
return response.json(); // Note: if we return a Promise, then there’s no need to await
156+
// Note: if we return a Promise, then there’s no need to await
157+
return response.json();
153158
}
154159

155160
async function main() {
156-
await fetchPerson('1');
157-
await fetchPerson('1');
158-
// We will see 'decoding data' twice now, as our function is run from scratch twice.
161+
await fetchPerson('1'); // *Creates* promise
162+
await fetchPerson('1'); // *Creates* another promise
163+
// We will see 'decoding data' logged twice,
164+
// as our function is run from scratch twice.
159165
}
160166

161167
main();
@@ -165,11 +171,12 @@ However, if we use the result from our `fetchPerson()` function (which we be a P
165171

166172
```javascript
167173
async function main() {
168-
const promise = fetchPerson('1');
174+
const promise = fetchPerson('1'); // *Creates* promise
169175

170176
await promise;
171177
await promise;
172-
// We will see 'decoding data' only once, as our function is run only once.
178+
// We will see 'decoding data' logged only once,
179+
// as our function is run only once.
173180
}
174181

175182
main();
@@ -188,12 +195,11 @@ function main() {
188195
// Do nothing
189196
});
190197

191-
// We will see 'decoding data' only once, as our function is run only once.
198+
// We will see 'decoding data' logged only once,
199+
// as our function is run only once.
192200
}
193201

194202
main();
195203
```
196204

197205
As we learned earlier, listening to a promise using `.then()` neither affects nor starts 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.
198-
199-

0 commit comments

Comments
 (0)