This commit is contained in:
31
content/posts/javascript-async-patterns.md
Normal file
31
content/posts/javascript-async-patterns.md
Normal file
@@ -0,0 +1,31 @@
|
||||
---
|
||||
title: "Modern JavaScript Async Patterns"
|
||||
date: 2024-08-12
|
||||
categories: ["tech", "tutorial"]
|
||||
tags: ["javascript", "async", "promises"]
|
||||
---
|
||||
|
||||
# Modern JavaScript Async Patterns
|
||||
|
||||
Exploring the evolution from callbacks to async/await and beyond.
|
||||
|
||||
## Promise Chains vs Async/Await
|
||||
|
||||
```javascript
|
||||
// Promise chains
|
||||
fetchUser(id)
|
||||
.then(user => fetchPosts(user.id))
|
||||
.then(posts => renderPosts(posts))
|
||||
.catch(handleError);
|
||||
|
||||
// Async/await
|
||||
try {
|
||||
const user = await fetchUser(id);
|
||||
const posts = await fetchPosts(user.id);
|
||||
renderPosts(posts);
|
||||
} catch (error) {
|
||||
handleError(error);
|
||||
}
|
||||
```
|
||||
|
||||
The async/await syntax makes asynchronous code much more readable!
|
||||
Reference in New Issue
Block a user