
A Horror Story About JavaScript Promise
Before we dive into our midnight adventure, let's quickly understand what Promises are in JavaScript. Don't worry - it's easier than facing a monster under the bed! What is a Promise? A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation. Think of it like a real-life promise: your friend promises to bring you coffee. Right now, you don't have the coffee - the promise is pending . Later, either you get the coffee ( fulfilled ) or your friend calls to say they spilled it ( rejected ). In JavaScript, a Promise is created using the new Promise() constructor, which takes a function with two parameters: resolve and reject . You call resolve(value) when the operation succeeds, and reject(error) when it fails. const coffeePromise = new Promise (( resolve , reject ) => { // Simulate making coffee const success = true ; if ( success ) { resolve ( " β Here's your coffee! " ); } else { reject ( " π Spilled the coffee... " ); } }); Then and Catch O
Continue reading on Dev.to Webdev
Opens in a new tab




