A promise is an object that is waiting for an asynchronous operation to complete, and when that operation completes, the promise is either fulfilled or rejected. A promise object can be any of these three states:

• fulfilled – when the promise succeeds

• rejected – when the promise fails

• pending – when it’s neither fulfilled or rejected

Creating a Promise

Promises are created using the new Promise() constructor that accepts an executor (a function) that takes two parameters:

• The first parameter (typically named resolve) is a function that is called with the future value when it's ready, that is, when the promise is fulfilled;

• And the second parameter (typically named reject) is a function that is called to reject the promise if it can't resolve the future value.

let condition = true;

function executor(resolve, reject) {
    if (condition) {
            resolve('ok');
    } else {
            reject('failure'); 
    }
}

const p = new Promise(executor).then(resolve => console.log(resolve));

new Promise(resolve => resolve()) // promise is fulfilled

new Promise((resolve, reject) => reject()) // promise is rejected

Usually, a promise will resolve to some value that could be a result from an HTTP request, animation, or some other asynchronous operation.

const p = new Promise((resolve, reject) => resolve(new Date()));

p.then((val) => console.log(val));

Every promise must have a .then() method that actually takes two possible parameters. The first parameter is the function to be called when the promise is fulfilled and the second parameter is a function to be called if the promise is rejected

const p = new Promise(constructor);

p.then(success, failure);


function constructor(resolve, reject) {
    resolve(new Date());
}

function success(value) { 
    console.log("Promise Fulfilled:", value); 
}

function failure(error) { 
    console.log("Promise Rejected: ", error); 
}

If a given promise always gets resolved, we can omit the second parameter for simplicity. Check out the following example where the customer gets his pizza five seconds after the order:

const pizza = new Promise((resolve) => {
    console.log("Getting your pizza in 5 seconds...");
    setTimeout(() => {
                 resolve("Onion Pizza");
    }, 5000);
});

pizza.then(
(item) => { console.log(`Order Received: ${item}`) },
(error) => { console.log("Something went wrong with your pizza") }
);

const pizza = new Promise(success);

pizza.then(thenSuccess, thenError);


function success(resolve) {
    console.log("Getting your pizza in 5 seconds...");
    setTimeout(() => {resolve("Onion Pizza");}, 5000);
}

function thenSuccess(item) { 
    console.log(`Order Received: ${item}`);
}

function thenError(error) { 
    console.log("Something went wrong with your pizza");
}

This example demonstrates two things:

• First, that the handlers we attached to the promise were called after all other code ran, asynchronously.

• Second, that the fulfillment handler was called only when the promise was fulfilled, with the value it was resolved with (in our case, the onion pizza). The same holds true for the rejection handler.

 const pizza = new Promise(success);

pizza.then(thenSuccess, thenError);


function success(resolve) {
    let client_Choice = "Tuna Pizza";
    console.log("Getting your pizza in 5 seconds...");
    setTimeout(() => {resolve(client_Choice);}, 5000);
}

function thenSuccess(client_Choice) { 
    console.log(`Order Received: ${client_Choice}`);
}

function thenError(error) { 
    console.log("Something went wrong with your pizza");
}

results matching ""

    No results matching ""