Member-only story
The Lowdown on Promises
A quick and concise guide to Promises in JavaScript
Mastering the concept of promises is a cornerstone of efficient JavaScript. It makes the task of dealing with asynchronous requests much easier and reduces the potential complexity of your code.
But what exactly are promises? How, when, and where would you use them? And how does promise chaining work?
Well, you’re in the right place to find out!
What Are JavaScript Promises?
The way JavaScript works is that it executes one thing and moves right onto the next. But sometimes you need the code to wait.
JavaScript’s natural flow doesn’t wait for anything. If the value that’s given isn’t right there when it needs it, it will be evaluated based on its current state and not what it’s supposed to be.
This means a variable could be temporary null
or undefined
— throwing your entire result out of whack.
JavaScript promises allows you to create code that can run in a separate and independent order than the usual flow of things.
Take a look at this diagram: