Skip to content

Commit d52380e

Browse files
committed
Expand Promise section
1 parent 097b30e commit d52380e

File tree

1 file changed

+61
-4
lines changed
  • apps/components_guide_web/lib/components_guide_web/templates/web_standards

1 file changed

+61
-4
lines changed

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

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
1-
# Promise
2-
3-
You can think of a Promise as a value. Once a Promise has been created, you can’t changed the value. Sooner or later its value will be given — as promised. (Or it may fail with an error. More on that later.)
1+
# Thinking about Promises
2+
3+
## Promises act as values
4+
5+
You can think of a Promise as an eventual value. It either succeeds with a particular success value, or it possibly fails with an error value.
6+
7+
<div class="flex text-3xl text-black bg-white">
8+
<div class="flex-grow p-4 bg-green-300 border border-green-400">
9+
<div>Resolve with <div class="px-2 text-white bg-black border-4 border-white border-dashed">Value</div></div>
10+
</div>
11+
<div class="flex-grow p-4 bg-orange-300 border-orange-400">
12+
<div>Reject with <div class="px-2 text-white bg-black border-4 border-white border-dashed">Error</div></div>
13+
</div>
14+
</div>
15+
16+
It either *resolves* to a success value, or it *rejects* with an error value.
17+
18+
<div class="my-4 flex text-3xl text-black bg-white">
19+
<div class="flex-grow p-4 bg-green-300 border border-green-400">
20+
<div>✅ Resolved with <div class="px-2 text-white bg-green-700 border-4 border-green-900 rounded">Value</div></div>
21+
</div>
22+
<div class="flex-grow p-4 bg-orange-900 border-orange-900">
23+
24+
</div>
25+
</div>
26+
27+
<div class="my-4 flex text-3xl text-black bg-white">
28+
<div class="flex-grow p-4 bg-green-900 border border-green-900">
29+
30+
</div>
31+
<div class="flex-grow p-4 bg-orange-300 border-orange-400">
32+
<div>✅ Rejected with <div class="px-2 text-white bg-orange-700 border-4 border-orange-900 rounded">Error</div></div>
33+
</div>
34+
</div>
35+
36+
Once a Promise has receive its value, you can’t changed that value. If it was told it failed, it can’t be changed to succeed instead. And if it succeeded, it can’t later fail.
37+
38+
## Chaining Promises
39+
40+
A Promise can create a _new_ Promise by calling `.then()` on it.
41+
42+
<div class="my-4 flex text-3xl text-black bg-white">
43+
<div class="flex-grow p-4 bg-green-300 border border-green-400">
44+
<div>✅ Resolved with <div class="px-2 text-white bg-green-700 border-4 border-green-900 rounded">Value</div></div>
45+
</div>
46+
<div class="flex-grow p-4 bg-orange-900 border-orange-900">
47+
48+
</div>
49+
</div>
50+
51+
<div class="text-center">.then</div>
52+
53+
<div class="my-4 flex text-3xl text-black bg-white">
54+
<div class="flex-grow p-4 bg-green-900 border border-green-900">
55+
56+
</div>
57+
<div class="flex-grow p-4 bg-orange-300 border-orange-400">
58+
<div>✅ Rejected with <div class="px-2 text-white bg-orange-700 border-4 border-orange-900 rounded">Error</div></div>
59+
</div>
60+
</div>
461

562
## Promises are eager
663

@@ -26,7 +83,7 @@ promisedValue.then(console.log);
2683
promisedValue.then(console.log);
2784
```
2885

29-
In both cases, we will see it logged only once. This is because promises are run once are created eagerly.
86+
In both cases, we will see it logged only once. This is because promises are run once and created eagerly.
3087

3188
Listening to a promise using `.then()` neither affects nor starts that promise. It has no side-effect on the source promise.
3289

0 commit comments

Comments
 (0)