Back to articles
JavaScript Promises Explained — With a Real Online Shopping Flow

JavaScript Promises Explained — With a Real Online Shopping Flow

via Dev.to BeginnersDHANRAJ S

Hey! You've probably seen the word Promise in JavaScript and thought — "What is this? Why does it exist? Why can't JavaScript just... do things normally?" Fair question. Let's fix that today. We're going to use a real online shopping flow to understand Promises — step by step, together. And by the end, this code will make complete sense to you. 1. What Is a Promise? (Before We Touch the Code) Imagine you order food online. The app doesn't freeze while your food is being made. You go about your day. When it's ready — you get a notification. That notification? That's a Promise resolving. In JavaScript, a Promise is an object that says: "I'll give you a result — but not right now. Give me some time. I'll get back to you." A Promise has 3 possible states: Pending — still working, no result yet Resolved — finished successfully, here's your result Rejected — something went wrong, here's the error Now let's see this in real code. 2. The Full Code — Look at It Once function checkInternet () {

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
2 views

Related Articles