Await Keyword Behavior
📖 Scenario: Imagine you are building a simple web app that fetches user data from a server. You want to learn how to wait for the data to arrive before using it.
🎯 Goal: You will create a function that simulates fetching data asynchronously and use the await keyword to wait for the data before printing it.
📋 What You'll Learn
Create an asynchronous function called
fetchUserData that returns a promise resolving to the string 'User data loaded' after 1 second.Create an asynchronous function called
showUserData.Inside
showUserData, use await to wait for fetchUserData to complete and store the result in a variable called data.Print the value of
data using console.log.💡 Why This Matters
🌍 Real World
Waiting for data from servers or APIs is common in web apps. Using <code>await</code> helps write clear code that waits for data before continuing.
💼 Career
Understanding <code>async</code> and <code>await</code> is essential for frontend and backend developers working with asynchronous data fetching.
Progress0 / 4 steps