codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Member-only story

Understanding Promises in JavaScript

Taran
codeburst
Published in
3 min readOct 6, 2020

--

What are promises and what is the difference between Promise.all, Promise.allSettled, Promise.race and Promise.any?

Introduction

Promises in JavaScript are used to handle asynchronous operations by keeping track of whether a certain event has happened. If that certain event has taken place, it determines what happens next. Promises return a value which is either a resolved value or a reason why it’s rejected. They can handle multiple asynchronous operations easily and they provide better error handling than callbacks and events.

Callback: A callback is a function that is passed into another function as an argument to be executed later.

Events: Events provide a dynamic interface to a WebPage and are connected to elements in the Document Object Model(DOM), for example: onclick(), onmouseover() etc.

A Promise has four states

Pending: Before the event has happened, the promise is in the pending state.

Settled: Once the event has happened it is then in the settled state.

Fulfilled: Action related to the promise has succeeded.

Rejected: Action related to the promise has failed.

An example of a Promise that resolves successfully:

An example of a rejected promise:

Comparison Table

Promise.all()

The Promise.all() method executes many promises in parallel. It accepts an array of promises and returns a single…

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Taran

I am a Full stack developer. I enjoy learning and building new skills, and sharing what I’ve learned.

No responses yet