Promise object

Promise object is used for handling asynchronous computations which has some important guarantees that are difficult to handle with the callback method (the more old-school method of handling asynchronous code).

A Promise object is simply a wrapper around a value that may or may not be known when the object is instantiated and provides a method for handling the value after it is known (also known as resolved) or is unavailable for a failure reason (we’ll refer to this as rejected).

Using a Promise object gives us the opportunity to associate functionality for an asynchronous operation’s eventual success or failure (for whatever reason). It also allows us to treat these complex scenarios by using synchronous.

When you create a new Promise, you’re really just creating a plain old JavaScript object. This object can invoke two methods, then, and catch. Both .then() and .catch() will return a new promise. That means that promises can be chained, with precise control over how and where errors are handled. Promises allow you to mimic normal synchronous code’s try/catch behavior.

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending. Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.

First note the first-principle constructor of a Promise – How to create and make a function that returns a Promise

const createdPromise = new Promise((resolve, reject) => {
  // do a thing, possibly async, then…

  if (/* everything turned out fine */) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});

First note the first-principle constructor of a Promise – How to create and make a function that returns a Promise

const createdPromise = new Promise((resolve, reject) => {
  // do a thing, possibly async, then…

  if (/* everything turned out fine */) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});

Now first Implementation – When Success case need to be returned

const promise = new Promise((resolve, reject) => {
  // do a thing, possibly async, then…

  if (1 === 1) {
    resolve("Stuff worked!");
  } else {
    reject(Error("It broke"));
  }
});

Will output the below

Stuff worked!

Now Second Implementation – When Error need to be returned

const promise = new Promise((resolve, reject) => {
  // do a thing, possibly async, then…

  if (1 === 2) {
    resolve("Stuff worked!");
  } else {
    reject(Error("It broke"));
  }
});

promise.then(
  result => {
    console.log(result); // "Stuff worked!"
  },
  function(err) {
    console.log(err); // Error: "It broke"
  }
);

The above will output the below

Error: It broke
    at Promise (/home/rohan/codeLap/js/challenges/challenges-May-19/JS-Python_Challenges/test-code-1.js:19:12)
    at new Promise (<anonymous>)
    at Object.<anonymous> (/home/rohan/codeLap/js/challenges/challenges-May-19/JS-Python_Challenges/test-code-1.js:12:17)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)

RxHarun
Logo