Modern Async
Async/Await is a modern way to work with promises that makes async code look and behave like synchronous code.
Key Concept: Instead of chaining .then(), you can write async code that reads like normal, top-to-bottom code!
fetchUser(1) .then(user => { console.log(user); return fetchPosts(user.id); }) .then(posts => { console.log(posts); }) .catch(error => { console.error(error); });
async function getData() { try { let user = await fetchUser(1); console.log(user); let posts = await fetchPosts(user.id); console.log(posts); } catch (error) { console.error(error); } } getData();