Member-only story
A Simple Beginner’s Guide to JavaScript Promises
Promises are a powerful tool in JavaScript but can take a while to understand and tend to confuse a lot of beginners. If you’re new to JavaScript, you might be wondering how to get started. That’s exactly what this article is for.
I’ll walk you through the basics of using promises in JavaScript and I’ll also cover how to handle errors that occur during the asynchronous operation, using the catch
method.
TL:DR
If you prefer to learn via videos, you can get the gist of this article in a 1-minute TikTok video.
A simple explanation
Imagine a Promise in JavaScript is like a waiter in a cafe. Imagine you order a sandwich from the waiter, the waiter writes down your order and goes to the kitchen to prepare it.
While you’re waiting for your food you can continue to do other things, like chat with your friend or check your phone. You don’t have to sit around waiting for the sandwich to be ready.
Eventually, the waiter will return. If waiter brings the sandwich to your table then we say that the promise is resolved. At this point, you can either choose to eat the sandwich, or you can decide not to eat it. However, if something has gone wrong during the preparation of your food and the waiter has…