1 view

Promise in JavaScript

Creating a Promise:

The task of the Promise is to handle such asynchronous operations. Now we are running the operation on a remote server, but until data arrives we cannot say that operation will not succeed or fail. And basically, the promise works to handle these. In most cases, we do not have to create our own promises. We handle Just Promise. The basic structure of the promise is that the library we work with will be implemented from the system. We have to handle just Promise. But I’ll also look at how we can create our own promise here.

    let bb = new Promise((resolve, reject) => {
      let a = 1 + 1;
      if(a == 2){
        resolve('Success man!')
      }else{
        reject('Faild man!')
      }
    })

    bb.then((message) => {
      console.log('Get', message)
    }).catch((message) =>{
      console.log('Get', message)
    })

Promise takes two arguments, resolveand reject.

Handle Promise:

Now we will handle whether the Promise has succeeded or failed. In that case, if the Promise is successful then we can chain our bb function and .then()give a callback function here, which resolvewill run if the Promise is successful or otherwise:

    bb.then((message) => {
      console.log('Get', message)
    }).catch((message) =>{
      console.log('Get', message)
    })

Callback

After that trueargument resolve, our callback will be triggered by the promise :

  bb.then((message) => {
      console.log('Get', message)
    })

And if the promise is rejected, then we need .catch()to handle it in another chain operation with a callback function. Now we do not know whether the operation will succeed or fail. In that case, we .then()have to .catch()keep both. .catch()If for any reason we will run reject:

.catch((message) =>{
      console.log('Get', message)
    })

The arguments falsegiven here rejectwill be from the promise . And so  catchthe callback will run inside the block after the operation is finished :

  let bb = new Promise((resolve, reject) => {
      let a = 1 + 2;
      if(a == 2){
        resolve('Success man!')
      }else{
        reject('Faild man!')
      }
    })

    bb.then((message) => {
      console.log('Get', message)
    }).catch((message) =>{
      console.log('Get', message)
    })

The bbfunction that we first returned the Promise here, the creation of the Promise, in most cases, does not require us to write. Rather, the system we use to exchange data, or use the library, is coded as a part of how to do resolveor not do rejectit. In most cases, the promise of our return .then(), and .catch()with the handle. The callbacks that are used inside, in most cases, the data we want from our remote server comes as arguments. We can access those arguments from within a callback.

Handling Multiple Promises: At times, we may have to handle multiple promos. For example, we have two premises:

      const a1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            resolve('a 1 resolved');
          }else{
            reject('a 1 error');
          }
        }, 3000)
      })

We can also directly create promises like this. Below I made another Promise:

      const a2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            if(true) {
              resolve('a 2 Resolved');
            } else {
              reject('a 2 Error');
            }
        }, 3000)
      })

Then .then()I can make a callback call which will run soon after these two promises are completed. And the data from these promos will be arrayed in this callback function:

Promise.all([a1, a2]).then((promiseArr) => {console.log(promiseArr)}) 

Leave a Reply