How to Resolve Callback Hells with Lovely JavaScript Promises

How to Resolve Callback Hells with Lovely JavaScript Promises

We have seen how JavaScript with Node.js promises asynchronous operations. Do you remember callback hell? So, the simplest solution is using our lovely Promises. A Promise is an object built on callbacks. How to Resolve Callback Hells with Lovely JavaScript Promises.

Make your promise…

In my last article, we have seen how JavaScript with Node.js performs asynchronous operations. Do you remember callbacks?

Callback is the backbone for asynchronous programming in Javascript. There are two main problems we face in using callbacks in our programming:

1. Callback hell

There are instances when we make a callback inside of a callback (called nested callbacks). It causes issues in code readability and understanding.

2. Difficulty in handling errors

When an asynchronous function or an asynchronously invoked callback throws an exception, there is no way for that exception to propagate back to the initiator of the asynchronous operation. It breaks the exception handling.

So, the simplest solution is using our lovely Promises.

A Promise is an object built on callbacks, which represents the result of an asynchronous computation. The result may or may not be ready. Promise works asynchronously only. There is no way of getting the value through synchronous calls. You can only ask the Promise to call a callback function when the value is ready.

Promises provide solutions for nested callbacks through re-expressing the nested callbacks in the form of a Promise chain. And through Promise, we can handle the errors in the proper way by handling them in the Promise chain.

A promise can be “kept” or “broken”.

Using the same logic, in JavaScript a Promise can be “fulfilled” or “rejected”.

There is another state there as well, that is “pending”. When a Promise is in a state where it’s neither been fulfilled nor rejected, it is pending. A Promise is settled once it is fulfilled or rejected.

programming javascript coding

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

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

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