Promises Summary
Promises represent future values in three states. Async/await is syntactic sugar over promises. Use Promise.all for parallel work, Promise.race for timeouts, AbortController for cancellation, and always handle errors.
Promises represent future values in three states. Async/await is syntactic sugar over promises. Use Promise.all for parallel work, Promise.race for timeouts, AbortController for cancellation, and always handle errors.
async function dashboard(userId) {
const [user, posts] = await Promise.all([
api.getUser(userId),
api.getPosts(userId),
]);
return { user, posts, loadedAt: Date.now() };
}
async/await reads synchronously but executes asynchronously — the best of both worlds.
More in JavaScript