-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathindex.html
284 lines (233 loc) · 10.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
</head>
<body>
<div class="vertical-split">
<div class="top">
<div class="horizontal-split">
<div class="left" id="top-left">
<h1>asynchronous<span class="js">JS</span>cheatsheet</h1>
<dl id="legend">
<dt>
<span class="promise pending">
<span class="bounce1"></span><span class="bounce2"></span><span class="bounce3"></span>
</span>
</dt>
<dd>
<strong>Pending</strong> promises can become either...
</dd>
<dt>
<span class="promise fulfilled">value</span>
</dt>
<dd>
<strong>Fulfilled</strong> with a <strong>value</strong>, or...
</dd>
<dt>
<span class="promise rejected">error</span>
</dt>
<dd>
<strong>Rejected</strong> with an <strong>error</strong>.
</dd>
<dt>
<span class="promise">outcome</span>
</dt>
<dd>
Either way, they are <strong>settled</strong> with an <strong>outcome</strong>.
</dd>
</dl>
</div>
<section id="combining" class="right">
<h2>Combining promises</h2>
<div class="columns">
<section>
<p>Use <code>all()</code> to turn an <em>array of promises</em> into a <em>promise to an array</em>.</p>
<div class="combine">
<pre><code>Promise.<strong>all</strong>([
<span class="fulfilled promise">value1</span>,
<span class="fulfilled promise">value2</span>,
<span class="fulfilled promise">value3</span>,
])
</code></pre>
<code class="arrow">-></code>
<pre><code><span class="fulfilled promise auto">[value1
value2,
value3]</span></code></pre>
</div>
</section>
<section>
<p>If <em>any</em> promise is rejected, the error will be passed through.</p>
<div class="combine">
<pre><code>Promise.<strong>all</strong>([
<span class="pending promise"><span class="bounce1"></span><span class="bounce2"></span><span class="bounce3"></span></span>,
<span class="pending promise"><span class="bounce1"></span><span class="bounce2"></span><span class="bounce3"></span></span>,
<span class="rejected promise">error</span>,
])
</code></pre>
<code class="arrow">-></code>
<code><span class="rejected promise">error</span></code>
</div>
</section>
<section>
<p>Use <code>race()</code> instead to pass through the first <em>settled</em> promise.</p>
<div class="combine">
<pre><code>Promise.<strong>race</strong>([
<span class="pending promise"><span class="bounce1"></span><span class="bounce2"></span><span class="bounce3"></span></span>,
<span class="pending promise"><span class="bounce1"></span><span class="bounce2"></span><span class="bounce3"></span></span>,
<span class="fulfilled promise">value</span>,
])
</code></pre>
<code class="arrow">-></code>
<code><span class="fulfilled promise">value</span></code>
</div>
</section>
</div>
</section><!-- right -->
</div><!-- horizontal-split -->
</div><!-- top -->
<div class="bottom">
<div class="horizontal-split">
<div class="right">
<div class="vertical-split">
<div class='top'>
<div class="horizontal-split">
<section id="then" class="left">
<h2>promise.<span class="keyword">then</span>( onFulfilled, onRejected )</h2>
<p>Calls <code>onFulfilled</code> once the promise is fulfilled.</p>
<code>
<span class="promise fulfilled">value</span>.<strong>then</strong>(<span class="function"><span class="value">value</span> => <span class="value">nextValue</span></span>, <span class="optional function">...</span>) -> <span class="promise fulfilled">nextValue</span>
</code>
<code>
<span class="promise fulfilled">value</span>.<strong>then</strong>(<span class="function"><span class="value">value</span> => <span class="promise">outcome</span></span>, <span class="optional function">...</span>) -> <span class="promise">outcome</span>
</code>
<code>
<span class="promise fulfilled">value</span>.<strong>then</strong>(<span class="function"><span class="value">value</span> => throw <span class="error">error</span></span>, <span class="optional function">...</span>) -> <span class="promise rejected">error</span>
</code>
<p>Calls <code>onRejected</code> if the promise is rejected.</p>
<code>
<span class="promise rejected">error</span>.<strong>then</strong>(<span class="optional function">...</span>, <span class="function"><span class="error">error</span> => <span class="value">value</span></span>) -> <span class="promise fulfilled">value</span>
</code>
<code>
<span class="promise rejected">error</span>.<strong>then</strong>(<span class="optional function">...</span>, <span class="function"><span class="error">error</span> => <span class="promise">outcome</span></span>) -> <span class="promise">outcome</span>
</code>
<code>
<span class="promise rejected">error</span>.<strong>then</strong>(<span class="optional function">...</span>, <span class="function"><span class="error">error</span> => throw <span class="error">nextError</span></span>) -> <span class="promise rejected">nextError</span>
</code>
<p>Passes errors through if <code>onRejected</code> is undefined.</p>
<code>
<span class="promise rejected">error</span>.<strong>then</strong>(<span class="function">...</span>) -> <span class="promise rejected">error</span>
</code>
</section><!-- left-->
<div class="right" id="non-then-handlers">
<section id="catch">
<h2>promise.<span class="keyword">catch</span>( onRejected )</h2>
<p>Behaves identically to <code>then</code> when <code>onFulfilled</code> is omitted.</p>
<code>
<span class="promise rejected">error</span>.<strong>catch</strong>(<span class="function"><strong>onRejected</strong></span>) <=> <span class="promise rejected">error</span>.<strong>then</strong>(<span class="optional function">...</span>, <span class="function"><strong>onRejected</strong></span>)
</code>
<p>Passes fulfilled values through.</p>
<code>
<span class="promise fulfilled">value</span>.<strong>catch</strong>(<span class="function">...</span>) -> <span class="promise fulfilled">value</span>
</code>
</section>
<section id="finally">
<h2>promise.<span class="keyword">finally</span>( onFinally )</h2>
<p>Calls <code>onFinally</code> with <em>no arguments</em> once any outcome is available. Passes through input promise.</p>
<code>
<span class="promise">outcome</span>.<strong>finally</strong>(<span class="function">() => ...</span>) -> <span class="promise">outcome</span>
</code>
<p class="warning">
The <code>onFulfilled</code>, <code>onRejected</code> and <code>onFinally</code> functions will not be executed until at least the <strong>next tick</strong>, even for promises that already have an outcome.
</p>
</section>
</div><!-- right -->
</div><!-- horizontal-split -->
</div><!-- top -->
<div class="bottom">
<section id="making-promises">
<h2>Making promises</h2>
<div class="horizontal-split">
<div class="left">
<p>The function passed to <code>new Promise</code> will be executed synchronously.</p>
<pre><code><strong>new Promise</strong>((<span class="green">resolve</span>, <span class="red">reject</span>) => {
doImportantStuff((<span class="error">error</span>, <span class="value">value</span>) => {
if (<span class="error">error</span>)
<span class="red">reject</span>(<span class="error">error</span>)
else
<span class="green">resolve</span>(<span class="value">value</span>)
})
})</code></pre>
</div>
<div class="right margin">
<section>
<p>Use <code>resolve()</code> or <code>reject()</code> to create promises from values.</p>
<code>Promise.<strong>resolve</strong>(<span class="value">value</span>) -> <span class="fulfilled promise">value</span></code>
<code>Promise.<strong>reject</strong>(<span class="error">error</span>) -> <span class="rejected promise">error</span></code>
<p>If you put a <em>fulfilled</em> promise into a <em>fulfilled</em> promise, they'll collapse into one.</p>
<code>Promise.<strong>resolve</strong>(<span class="fulfilled promise">value</span>) -> <span class="fulfilled promise">value</span></code>
</section>
<hr />
<p>Sometimes you might not need <code>reject</code>, or might not resolve to a value.</p>
<pre><code>function delay(milliseconds) {
return <strong>new Promise</strong>(<span class="green">resolve</span> =>
setTimeout(<span class="green">resolve</span>, milliseconds)
)
}</code></pre>
</div>
</div>
</section>
</div><!-- bottom -->
</div><!-- vertical-split -->
</div><!-- right -->
<section id="async-await" class="left">
<h2><span class="keyword">async</span>/<span class="keyword">await</span></h2>
<p>Calling an <code>async</code> function <em>always</em> results in a promise.</p>
<code>
<span class="function">(<strong>async</strong> () => <span class="value">value</span>)</span>() -> <span class="fulfilled promise">value</span>
</code>
<code>
<span class="function">(<strong>async</strong> () => <span class="promise">outcome</span>)</span>() -> <span class="promise">outcome</span>
</code>
<code>
<span class="function">(<strong>async</strong> () => throw <span class="error">error</span>)</span>() -> <span class="rejected promise">error</span>
</code>
<p><code>await</code> waits for a promise to be fulfilled, then returns its value.</p>
<pre><code><strong>async</strong> function() {
try {
let <span class="value">value</span> = <strong>await</strong> <span class="promise">outcome</span>
// ...
}
catch (<span class="error">error</span>) {
// ...
}
}
</code></pre>
<p>You can pass non-promise values to <code>await</code></p>
<pre><code>const fn = <strong>async</strong> () => {
let <span class="value">value</span> = <strong>await</strong> <span class="value">value</span>
// ...
}
</code></pre>
<p class="warning">
<code>await</code> may only be used within <code>async</code> functions.
</p>
<p class="warning">
<code>await</code> will wait until at least the <strong>next tick</strong> before returning, even when awaiting already-fulfilled promises or non-promise values.
</p>
<footer>
<div class="brand">
<img src="logo.png" alt="Frontend Armory Logo" />
<span class="frontend">Frontend</span> <span class="armory">Armory</span>
</div>
<div class="url">
frontarm.com
</div>
</footer>
</section> <!-- left -->
</div><!-- horizontal-split -->
</div><!-- bottom -->
</div><!-- vertical-split -->
</body>
</html>