Creating promises
📖 Scenario: You are building a simple app that simulates checking if a user is logged in. This check takes some time, so you will use a promise to handle the waiting.
🎯 Goal: Create a promise that waits 2 seconds and then resolves with the message "User is logged in". Then display this message.
📋 What You'll Learn
Create a promise called
loginCheck that waits 2 seconds before resolvingUse
setTimeout inside the promise executor functionResolve the promise with the exact string
'User is logged in'Use
loginCheck.then to get the resolved messagePrint the resolved message using
console.log💡 Why This Matters
🌍 Real World
Promises are used in web apps to handle tasks like loading data from servers, waiting for user input, or running animations smoothly.
💼 Career
Understanding promises is essential for frontend and backend JavaScript developers to write clean, readable, and efficient asynchronous code.
Progress0 / 4 steps