The difference between Promise and Thenable in JavaScript

Here's a question I thought I knew the answer to:  What does await really wait for? My answer was always "a Promise." I was shocked to find out I was wrong. await (and Promise.resolve()) doesn't actually check if an object is an instanceof Promise. It just asks one simple question: "Does this object have a .then() method?" If yes, it's treated as a "thenable," and await will happily wait for it. Promise: The official, native object with .then(), .catch(), etc. Thenable: Anything that has a .then() method. So basically: ✅ Every Promise is a Thenable ❌ But not every Thenable is a Promise This is the "duck typing" magic that makes the entire JavaScript async ecosystem work together. Old promise-like libraries can seamlessly interoperate with modern async/await because they all speak the common language of .then(). This simple distinction was a huge "aha" moment for me. 🤯 Did you already know this, or is your mind just as blown as mine was? It’s one of those small things that shows how deep JavaScript really is. No matter how much you learn, there’s always something new waiting to surprise you. 💡 #JavaScript #WebDevelopment #AsyncProgramming #LearningEveryday #Async #Promises #CodingTips

  • diagram, schematic

To view or add a comment, sign in

Explore content categories