Skip to main content

Command Palette

Search for a command to run...

Understanding Promise by Singer Analogy

Published
3 min read
Understanding Promise by Singer Analogy

one singer who went viral for his songs. He got lakho followers , his fan demand for new song. his fan always excited and wait for his concert, new release so, he promised his fan to release song in his channel. He gave channel name and his fan subscribed it. Now whenever The singer released it will show in the channel so this is the promise. It is object which is placeholder for future value. It has two things: state and results.

Initially It is pending state. first singer write the song then after record in studio and plan to release date. This is the process time so In this state the results is undefined . There is no new song in the channel.

After record and planned the date , the song is ready to released and the song released . This is the fulfilled state. and the results value is new song which fans or viewers can see and listen in his channel.

but when there comes some technical issues and some disputes between the director and the singer then the song could not get to release on time then this is rejected state . In this case the singer may post sorry message on channel post or may come live to say sorry to his fans.

Now lets learn about some methods in promise by the excitement of fans.

const singerPromise = new Promise((resolve, reject) => {
  const songReady = true;
  const conflictWithDirector = false;

  if (songReady && !conflictWithDirector) {
    resolve("Song released on channel 🎶");
  } else {
    reject("Song release failed due to issues 😢");
  }
});

singerPromise
  .then((message) => console.log("Fulfilled:", message))
  .catch((error) => console.log("Rejected:", error));

Some Methods

i. Promise.all

All fans are excited for new album release. if all songs come at once in the channel then it is fulfilled state. if any song may not release the it ruins the experience of fans to listen the album so it is rejected state.

const song1 = Promise.resolve("Track 1 released");
const song2 = Promise.resolve("Track 2 released");
const song3 = Promise.reject("Track 3 failed");

Promise.all([song1, song2, song3])
  .then((messages) => console.log("Album released:", messages))
  .catch((error) => console.log("Album failed:", error));

ii. Promise.any

The fans become happy on the first drop of a song of the album. it is fulfilled state . it does not matter for other songs released or fail to release.

const song1 = Promise.reject("Track 1 failed");
const song2 = Promise.resolve("Track 2 released");
const song3 = Promise.reject("Track 3 failed");

Promise.any([song1, song2, song3])
  .then((message) => console.log("Fans are happy with:", message))
  .catch((error) => console.log("No song released:", error));

iii. Promise.allSettled

This is the total records of the song . the fans or audience get to know which song is released or fail .

const song1 = Promise.resolve("Track 1 released");
const song2 = Promise.reject("Track 2 failed");
const song3 = Promise.resolve("Track 3 released");

Promise.allSettled([song1, song2, song3])
  .then((results) => {
    results.forEach((result, index) => {
      console.log(`Track ${index + 1}:`, result.status, result.reason || result.value);
    });
  });

iv. Promise.race

It is like race . which song released first got millions of views so The first released determines the outcomes .

const song1 = new Promise((resolve) => setTimeout(resolve, 3000, "Track 1 released"));
const song2 = new Promise((resolve) => setTimeout(resolve, 1000, "Track 2 released"));

Promise.race([song1, song2])
  .then((message) => console.log("First song out:", message));

Summary

So this is the singer analogy to understand the promise in JavaScript in easy way.