Promises, async/await, and error handling
| Command | Description |
|---|---|
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 |
| Command | Description |
|---|---|
async function fn() {} | Declare async function |
const data = await promise | Pause execution until resolved |
const fn = async () => {} | Async arrow function |
(async () => {})() | IIFE async |
| Command | Description |
|---|---|
try { await fn() } catch (err) {} | Handle async errors |
catch (err) { console.error(err) } | Log error |
finally { setLoading(false) } | Cleanup code |