Back to Cheatsheets
JavaScript

Async JavaScript

Promises, async/await, and error handling

Promises

7 items
CommandDescription
new Promise((resolve, reject) => {})
Create promise
promise.then(data => {})
Handle success
promise.catch(err => {})
Handle error
promise.finally(() => {})
Run always (cleanup)
Promise.all([p1, p2])
Wait for all (fail fast)
Promise.allSettled([p1, p2])
Wait for all (no fail)
Promise.race([p1, p2])
First to settle wins

Async / Await

4 items
CommandDescription
async function fn() {}
Declare async function
const data = await promise
Pause execution until resolved
const fn = async () => {}
Async arrow function
(async () => {})()
IIFE async

Try / Catch

3 items
CommandDescription
try { await fn() } catch (err) {}
Handle async errors
catch (err) { console.error(err) }
Log error
finally { setLoading(false) }
Cleanup code