
JavaScript Promises Explained With Simple Real Examples
Promises confused me for a long time. I kept reading definitions like "a promise is an object that represents the eventual completion or failure of an async operation" and I just stared at the screen. So let me explain it the way I wish someone explained it to me. What is a Promise in simple words Think about ordering food at a restaurant. You order, the waiter says okay and walks away. You don't sit there frozen waiting. You talk to your friends, drink water, do other things. When the food is ready the waiter comes back either with your food or says sorry we ran out of that. That is a Promise. You asked for something. It is being worked on. Either it succeeds or it fails. Meanwhile everything else keeps going. How it looks in code const myPromise = new Promise ( function ( resolve , reject ) { let success = true ; if ( success ) { resolve ( " it worked " ); } else { reject ( " something went wrong " ); } }); resolve means it went well. reject means something failed. How to use the res
Continue reading on Dev.to Webdev
Opens in a new tab

