├── LICENSE.md ├── README.md ├── async-cheatsheet.png ├── index.html ├── logo.png └── stylesheet.css /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-present James K Nelson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Asynchronous JavaScript cheatsheet 2 | 3 | This cheatsheet contains a summary of the [Mastering Asynchronous JavaScript](https://frontarm.com/courses/async-javascript/) course at [Frontend Armory](https://frontarm.com/browse/). 4 | 5 | Want to deepen your understanding of promises, `async` and `await`? The course will guide you there through **47 live examples and exercises.** To get started, just click through to the first lesson: [Why async, anyway?](https://frontarm.com/courses/async-javascript/promises/why-async/). 6 | 7 | ![Asynchronous JavaScript Cheatsheet](./async-cheatsheet.png) 8 | 9 | Want a printable-optimized PDF version but don't want to build it yourself? There's one included in [the course](https://frontarm.com/courses/async-javascript/in-practice/cheatsheet/). -------------------------------------------------------------------------------- /async-cheatsheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frontarm/async-javascript-cheatsheet/a01638d8a4ba8662f5f10543f337a75b99cf0f87/async-cheatsheet.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 |
10 | 11 |
12 |
13 | 14 |

asynchronousJScheatsheet

15 |
16 |
17 | 18 | 19 | 20 |
21 |
22 | Pending promises can become either... 23 |
24 | 25 |
26 | value 27 |
28 |
29 | Fulfilled with a value, or... 30 |
31 | 32 |
33 | error 34 |
35 |
36 | Rejected with an error. 37 |
38 | 39 |
40 | outcome 41 |
42 |
43 | Either way, they are settled with an outcome. 44 |
45 |
46 | 47 |
48 | 49 |
50 |

Combining promises

51 |
52 |
53 |

Use all() to turn an array of promises into a promise to an array.

54 |
55 |
Promise.all([
 56 |   value1,
 57 |   value2,
 58 |   value3,
 59 | ])
 60 | 
61 | -> 62 |
[value1
 63 |   value2,
 64 |   value3]
65 |
66 |
67 | 68 |
69 |

If any promise is rejected, the error will be passed through.

70 |
71 |
Promise.all([
 72 |   ,
 73 |   ,
 74 |   error,
 75 | ])
 76 | 
77 | -> 78 | error 79 |
80 |
81 | 82 |
83 |

Use race() instead to pass through the first settled promise.

84 |
85 |
Promise.race([
 86 |   ,
 87 |   ,
 88 |   value,
 89 | ])
 90 | 
91 | -> 92 | value 93 |
94 |
95 |
96 |
97 | 98 |
99 | 100 |
101 | 102 |
103 | 104 |
105 |
106 | 107 |
108 |
109 | 110 |
111 | 112 |
113 |

promise.then( onFulfilled, onRejected )

114 | 115 |

Calls onFulfilled once the promise is fulfilled.

116 | 117 | 118 | value.then(value => nextValue, ...) -> nextValue 119 | 120 | 121 | value.then(value => outcome, ...) -> outcome 122 | 123 | 124 | value.then(value => throw error, ...) -> error 125 | 126 | 127 |

Calls onRejected if the promise is rejected.

128 | 129 | 130 | error.then(..., error => value) -> value 131 | 132 | 133 | error.then(..., error => outcome) -> outcome 134 | 135 | 136 | error.then(..., error => throw nextError) -> nextError 137 | 138 | 139 |

Passes errors through if onRejected is undefined.

140 | 141 | 142 | error.then(...) -> error 143 | 144 | 145 |
146 | 147 |
148 | 149 |
150 |

promise.catch( onRejected )

151 | 152 |

Behaves identically to then when onFulfilled is omitted.

153 | 154 | 155 | error.catch(onRejected) <=> error.then(..., onRejected) 156 | 157 | 158 |

Passes fulfilled values through.

159 | 160 | 161 | value.catch(...) -> value 162 | 163 |
164 | 165 |
166 |

promise.finally( onFinally )

167 | 168 |

Calls onFinally with no arguments once any outcome is available. Passes through input promise.

169 | 170 | 171 | outcome.finally(() => ...) -> outcome 172 | 173 | 174 |

175 | The onFulfilled, onRejected and onFinally functions will not be executed until at least the next tick, even for promises that already have an outcome. 176 |

177 |
178 | 179 |
180 |
181 | 182 |
183 |
184 | 185 |
186 |

Making promises

187 |
188 |
189 |

The function passed to new Promise will be executed synchronously.

190 |
new Promise((resolve, reject) => {
191 |   doImportantStuff((error, value) => {
192 |     if (error)
193 |       reject(error)
194 |     else
195 |       resolve(value)
196 |   })
197 | })
198 |
199 |
200 |
201 |

Use resolve() or reject() to create promises from values.

202 | Promise.resolve(value) -> value 203 | Promise.reject(error) -> error 204 |

If you put a fulfilled promise into a fulfilled promise, they'll collapse into one.

205 | Promise.resolve(value) -> value 206 |
207 |
208 |

Sometimes you might not need reject, or might not resolve to a value.

209 |
function delay(milliseconds) {
210 |   return new Promise(resolve =>
211 |     setTimeout(resolve, milliseconds)
212 |   )
213 | }
214 |
215 |
216 |
217 | 218 |
219 |
220 | 221 |
222 | 223 |
224 |

async/await

225 | 226 |

Calling an async function always results in a promise.

227 | 228 | 229 | (async () => value)() -> value 230 | 231 | 232 | (async () => outcome)() -> outcome 233 | 234 | 235 | (async () => throw error)() -> error 236 | 237 | 238 |

await waits for a promise to be fulfilled, then returns its value.

239 | 240 |
async function() {
241 |   try {
242 |     let value = await outcome
243 |     // ...
244 |   }
245 |   catch (error) {
246 |     // ...
247 |   }
248 | }
249 | 
250 | 251 |

You can pass non-promise values to await

252 | 253 |
const fn = async () => {
254 |   let value = await value
255 |   // ...
256 | }
257 | 
258 | 259 |

260 | await may only be used within async functions. 261 |

262 | 263 |

264 | await will wait until at least the next tick before returning, even when awaiting already-fulfilled promises or non-promise values. 265 |

266 | 267 |
268 |
269 | Frontend Armory Logo 270 | Frontend Armory 271 |
272 |
273 | frontarm.com 274 |
275 |
276 | 277 |
278 | 279 |
280 | 281 |
282 |
283 | 284 | 285 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frontarm/async-javascript-cheatsheet/a01638d8a4ba8662f5f10543f337a75b99cf0f87/logo.png -------------------------------------------------------------------------------- /stylesheet.css: -------------------------------------------------------------------------------- 1 | @import url(https://cdn.jsdelivr.net/gh/tonsky/FiraCode@1.206/distr/fira_code.css); 2 | @page { size: A4 landscape; margin: 1cm } 3 | 4 | * { 5 | box-sizing: border-box; 6 | } 7 | 8 | html { 9 | font-family: 'Lato'; 10 | font-size: 8pt; 11 | } 12 | 13 | body { 14 | --border-radius: 2pt; 15 | 16 | --black: #342656; 17 | --light-black: #342656; 18 | 19 | --darker-grey: #7272a3; 20 | --dark-grey: #8a8ab5; 21 | --grey: #A9A9C9; 22 | --light-grey: #dae1f2; 23 | --lighter-grey: #ecf3fc; 24 | 25 | --green: #12c8ba; 26 | 27 | --red: #dd3c6f; 28 | --light-red: #FDF0F5; 29 | 30 | color: var(--black); 31 | margin: 1rem; 32 | } 33 | code { 34 | font-family: 'Fira Code'; 35 | } 36 | .green { 37 | color: var(--green); 38 | } 39 | .red { 40 | color: var(--red); 41 | } 42 | 43 | #top-left { 44 | width: 26.5rem 45 | } 46 | #top-left, #async-await, #then { 47 | margin-right: 2rem; 48 | } 49 | #then { 50 | width: 39rem; 51 | } 52 | #non-then-handlers { 53 | width: 39rem; 54 | } 55 | #async-await { 56 | width: 26.5rem; 57 | } 58 | #combining { 59 | margin-top: -0.33rem; 60 | flex-shrink: 0; 61 | width: 80rem; 62 | } 63 | #combining h2 { 64 | margin-bottom: 0; 65 | } 66 | #combining .columns { 67 | display: flex; 68 | justify-content: space-between; 69 | } 70 | #combining .columns > section { 71 | margin-right: 1rem; 72 | } 73 | #combining .columns > section:last-child { 74 | margin-right: 0; 75 | } 76 | #making-promises h2 { 77 | margin-top: 3.5rem; 78 | margin-bottom: 0; 79 | } 80 | #making-promises .horizontal-split > div { 81 | flex-grow: 1; 82 | } 83 | .margin { 84 | margin-left: 4rem; 85 | } 86 | hr { 87 | margin: 1rem 0; 88 | border: none; 89 | border-bottom: 1px dotted var(--light-grey); 90 | } 91 | 92 | .horizontal-split { 93 | display: flex; 94 | flex-direction: row; 95 | justify-content: space-between; 96 | } 97 | .vertical-split { 98 | display: flex; 99 | flex-direction: column; 100 | } 101 | .left, .top { 102 | order: 0; 103 | flex-shrink: 0; 104 | } 105 | .right, .bottom { 106 | order: 1; 107 | flex-shrink: 0; 108 | } 109 | 110 | h1 { 111 | font-weight: 900; 112 | color: #aaa; 113 | padding: 0 10px; 114 | margin: 0; 115 | font-size: 15pt; 116 | color: var(--black); 117 | } 118 | h1 .js { 119 | display: inline-block; 120 | background-color: #FFE528; 121 | font-size: 2rem; 122 | height: 3rem; 123 | width: 3rem; 124 | line-height: 2rem; 125 | padding: 1rem 0.25rem 0rem 0.25rem ; 126 | text-align: right; 127 | vertical-align: bottom; 128 | } 129 | 130 | h2 { 131 | background-color: var(--light-black); 132 | color: white; 133 | line-height: 2rem; 134 | font-size: 1.1rem; 135 | padding: 0 0.5rem; 136 | border-radius: var(--border-radius); 137 | margin-top: 2rem; 138 | margin-bottom: 0.75rem; 139 | color: rgba(255, 255, 255, 0.63); 140 | } 141 | h2 .keyword { 142 | color: rgba(255, 255, 255, 1); 143 | } 144 | p { 145 | color: var(--black); 146 | line-height: 1.25rem; 147 | font-size: 1rem; 148 | margin: 1rem 0 0.75rem; 149 | } 150 | p > code { 151 | color: var(--darker-grey); 152 | font-weight: 500; 153 | } 154 | section > code, section > pre { 155 | display: block; 156 | margin: 0.375rem 0; 157 | } 158 | section > code, pre > code { 159 | color: var(--darker-grey); 160 | } 161 | section > pre { 162 | margin: 1rem 0 0.75rem; 163 | } 164 | section > code strong, pre > code strong { 165 | color: var(--light-black); 166 | font-weight: normal !important; 167 | } 168 | 169 | .combine { 170 | display: flex; 171 | align-items: center; 172 | } 173 | .combine pre { 174 | background: none; 175 | padding: 0; 176 | margin: 0; 177 | } 178 | .combine .arrow { 179 | margin: 0 1rem 0 0; 180 | } 181 | .combine .auto.promise { 182 | height: auto; 183 | } 184 | 185 | pre, .warning { 186 | border-radius: var(--border-radius); 187 | padding: 0.5rem; 188 | } 189 | pre { 190 | background-color: var(--lighter-grey); 191 | } 192 | .warning { 193 | background-color: var(--light-red); 194 | position: relative; 195 | } 196 | .warning::before { 197 | content: "!"; 198 | color: var(--red); 199 | border: 1px solid var(--red); 200 | display: block; 201 | border-radius: 0.5rem; 202 | font-weight: bold; 203 | line-height: 1.05rem; 204 | text-align: center; 205 | height: 1.1rem; 206 | width: 1.1rem; 207 | float: right; 208 | } 209 | 210 | dt { 211 | float: left; 212 | width: 22%; 213 | text-align: right; 214 | padding: .25rem; 215 | clear: left; 216 | margin: 0.25rem 0; 217 | } 218 | dd { 219 | float: left; 220 | margin-left: 2%; 221 | line-height: 1.5rem; 222 | width: 76%; 223 | padding: .25rem 0; 224 | margin: 0.25rem 0 0.25rem 0.25rem; 225 | } 226 | dl:after {content:"";display:table;clear:both;} 227 | 228 | .function { 229 | background-color: var(--lighter-grey); 230 | border-radius: 1rem; 231 | display: inline-block; 232 | height: 2rem; 233 | line-height: 2rem; 234 | padding: 0 0.5rem; 235 | } 236 | .optional { 237 | opacity: 0.5; 238 | } 239 | .optional::after { 240 | content: "?"; 241 | margin-left: -0.4rem; 242 | } 243 | .error { 244 | color: var(--red); 245 | font-weight: bold; 246 | } 247 | .value { 248 | color: var(--green); 249 | font-weight: bold; 250 | } 251 | 252 | .promise { 253 | border-radius: var(--border-radius); 254 | border-width: 1px; 255 | border-style: solid; 256 | display: inline-block; 257 | height: 1.5rem; 258 | font-family: 'Fira Code'; 259 | font-weight: bold; 260 | line-height: 1.5rem; 261 | margin: 0.1rem 0; 262 | padding: 0 0.35rem; 263 | margin-right: 0.15rem; 264 | 265 | border-color: var(--grey); 266 | color: var(--dark-grey); 267 | } 268 | 269 | .fulfilled { 270 | border-color: var(--green); 271 | color: var(--green); 272 | } 273 | .rejected { 274 | border-color: var(--red); 275 | color: var(--red); 276 | } 277 | 278 | #legend .pending { 279 | padding-top: 0.3rem; 280 | } 281 | 282 | .pending { 283 | display: inline-block; 284 | text-align: center; 285 | width: 4rem; 286 | padding: 0.25rem; 287 | } 288 | .pending > span { 289 | margin: 0 0.05rem; 290 | width: 0.75rem; 291 | height: 0.75rem; 292 | background-color: var(--light-grey); 293 | 294 | border-radius: 100%; 295 | display: inline-block; 296 | animation: sk-bouncedelay 1.4s infinite ease-in-out both; 297 | } 298 | .pending .bounce1 { 299 | animation-delay: -0.32s; 300 | } 301 | .pending .bounce2 { 302 | animation-delay: -0.16s; 303 | } 304 | 305 | .bounce1, .bounce3 { 306 | opacity: 0.5; 307 | } 308 | /* 309 | Thanks spinkit! http://tobiasahlin.com/spinkit/ 310 | @keyframes sk-bouncedelay { 311 | 0%, 80%, 100% { 312 | -webkit-transform: scale(0); 313 | transform: scale(0); 314 | } 40% { 315 | -webkit-transform: scale(1.0); 316 | transform: scale(1.0); 317 | } 318 | } */ 319 | 320 | footer { 321 | margin-top: 5rem; 322 | } 323 | .brand, .url { 324 | display: inline-block; 325 | color: var(--black); 326 | } 327 | .brand img { 328 | height: 4rem; 329 | vertical-align: -1.5rem; 330 | margin-right: 1rem; 331 | } 332 | .brand span { 333 | text-transform: uppercase; 334 | font-size: 1.5rem; 335 | } 336 | .brand .frontarm { 337 | font-weight: 100; 338 | } 339 | .brand .armory { 340 | font-weight: 900; 341 | } 342 | footer .url { 343 | margin-left: 5.2rem; 344 | margin-top: -1rem; 345 | color: var(--grey); 346 | } 347 | --------------------------------------------------------------------------------