JavaScript Promises Are Easier Than You Think

JavaScript Promises Are Easier Than You Think

When I encounter a new abstract concept in programming, I always try to relate it to something easy and quite understandable, like food 😋

When I encounter a new abstract concept in programming, I always try to relate it to something easy and quite understandable, like food 😋

I LOVE food! and almost everyone that I met does. That’s why today we are going to think about Promises and Asynchronous code execution in JavaScript as some tasty meals of food. I tried this with my students at RBK and it worked like charm. That’s why I’m going to relate asynchronous code execution to ordering a meal from a restaurant. Now, let’s go ahead and order some promises.Promises and Foo

It’s lunch time, you’re starving. You don’t know how to cook and you don’t even have time to cook. That’s why you decided to grab lunch from the McDonald restaurant around the corner. You’ve always loved the cheeseburger they make so your decision wasn’t hard at all.

You go to the cashier and ask him for a cheeseburger. You pay for your meal and in return, he gives you back a ticket. Now that ticket right there is the JavaScript promise. It’s not food, it’s not a value, but it’s a future value. The cashier (or the waiter) gave you a promise that he is going to send you back your meal. When? it can take a few seconds or minutes just as it can take an hour. It all depends on how quick are the cookers and how many people are already waiting in the queue. In the worst-case scenario, the waiter will come at you and tell you that they ran out of cheese 😫 Maybe then you can order something else.

The same goes for promises, you execute some asynchronous code (like sending a request to the server from the client-side) and you wait for your response. Meanwhile, you’ll get a promise to future data in return.

When the burger (or the data) is finally ready, you can then start eating it and enjoying the taste. Otherwise, you may order something else or visit another restaurant that still has some cheese left.

The same goes for promises. If you get back your response you can manipulate that data or output it to the user. On the other hand, if you don’t, you do something else like informing the user that there is no data left.

coding programming javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.

Code Formatting with Prettier in Visual Studio Code

Formatting code consistently is a pain, especially when working on a team. The beauty of modern-day web development is that the tooling has gotten so much better! In this article, we will look at setting up Prettier to automatically format your code in Visual Studio Code.

Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Don’t waste time writing long code while you can make it short, yet clearer and easier to read. In this Javascript tutorial, we'll discuss 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript