-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added async exercise and slightly reworked button styling
- Loading branch information
Kevin Lu
committed
Sep 20, 2024
1 parent
3111b77
commit d752227
Showing
2 changed files
with
39 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -547,4 +547,35 @@ findUser() | |
console.log("Age:", age + 5); | ||
}) | ||
.catch((error) => console.error("Error:", error.message)); | ||
``` | ||
``` | ||
|
||
## Exercise 4: Async await practice | ||
|
||
Create an async function fetchData that simulates an API call to fetch user data. The function should return a promise that resolves after 2 seconds with a user object containing name and email. | ||
|
||
Then, write another async function displayUser that uses await to call fetchData and logs the user's information to the console. | ||
|
||
<ToggleCodeBlock> | ||
```javascript copy showLineNumbers | ||
// Simulate API call using a Promise that resolves after 2 seconds | ||
async function fetchData() { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ name: "John Doe", email: "[email protected]" }); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
// Use async/await to fetch and display user data | ||
async function displayUser() { | ||
try { | ||
const user = await fetchData(); | ||
console.log("User Name:", user.name); | ||
console.log("User Email:", user.email); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
// Call the displayUser function displayUser(); | ||
``` | ||
</ToggleCodeBlock> |
17 changes: 7 additions & 10 deletions
17
pages/hack-school/toggle_component.jsx → pages/hack-school/toggle_component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters