Introduction to JavaScript Promises

"The Basics of JavaScript Promises"


Promises in JavaScript are objects that represent the eventual completion or failure of an asynchronous operation. They provide a simpler, more powerful way of dealing with asynchronous behavior than callbacks or event listeners. When a promise is created, it is in one of three states: pending, fulfilled, or rejected. When the asynchronous operation is complete, the promise is either fulfilled with a value, or rejected with a reason. When that happens, any callback functions associated with the promise are called. Promises allow developers to write asynchronous code that is easier to read and debug.


Promises are created by Promise constructors, which take an executor function as an argument. This executor function is called immediately with two parameters: a resolve function and a reject function. The resolve and reject functions are used to control the state of the promise. When the asynchronous operation is complete, the executor calls one of these functions with a value. This causes the promise to transition to either the fulfilled or rejected state.

Promises can also be chained together, allowing for the creation of complex asynchronous flows. When a promise is fulfilled, it can return a new promise. This allows for the creation of a promise chain, which allows for the sequential execution of asynchronous operations. The value returned by the promise's then method will be the value of the new promise.

Promises also support error handling. When an error occurs, the promise will be rejected with the error. This allows for the creation of a catch block, which will be called if the promise is rejected. This allows for better error handling and makes debugging easier.

Promises are one of the most powerful features of modern JavaScript. They allow developers to write asynchronous code that is easier to read and debug. They make error handling easier, and allow for the creation of complex asynchronous flows. Promises are an essential part of modern JavaScript, and are used widely in web applications and Node.js applications.


Syntax: let promise = new Promise(function(resolve, reject) { // do a thing, possibly async, then… if (/* everything turned out fine */) { resolve("Stuff worked!"); }
else { reject(Error("It broke")); } });

Example:1

let promise = new Promise
  (function(resolve, reject) {
    let request = new XMLHttpRequest();

 request.open
('GET', 'https://www.example.com/api/data');
    request.onload = function() {
        if (request.status == 200) {
            // Success
            resolve(request.response);
        } else {
          // Something went wrong (404 etc.)
          reject(Error(request.statusText));
        }
    };
    request.send();
});

Example:2

function prom(complete){
return new Promise(function(resolve,reject){

       if(complete){
         resolve("this is the successful");

          }else{
              reject("this is rejected");
                }
            });
        }

        let onfullfillment=(result)=>{
            console.log(result);
        }

        let onrejection=(error)=>{
            console.log(error);
        }
       
        prom(true).then(onfullfillment);
        prom(false).catch(onrejection);

                                  
                                                                                                  

No comments:

Post a Comment