creating promise 2

 let cart = ["shoes", "pants", "kurtas"];


createOrder(cart)
    .then(function(res) {
        console.log(res);
        // promise chaining
        return res;
    })
    .then(function(res) {
        return proceedToPayment(res);
    })
    .then(function(paymentInfo) {
        console.log(paymentInfo);
    })
    .catch(err => {
        console.log(err.message);
    });

// start api which is running to order creation:---
function createOrder(cart) {
    const myPromise = new Promise(function(resolve, reject) {
        if (!validiteCart(cart)) {
            const err = new Error('Cart is not valid');
            reject(err);
        } else {
            const orderId = '123';
            setTimeout(() => {
                resolve(orderId);
            }, 5000);
        }
    });
    return myPromise;
}

function proceedToPayment(orderId) {
    // we will do payment
    return new Promise((resolve, reject) => {
        resolve("Payment successful");
    });
}

// end api which is running to order creation:--
// proceed to payment api:--
// end of proceed to payment api:--

function validiteCart(cart) {
    return true; // Assume the cart is always valid for this example
    // return false;
}

Comments

Popular posts from this blog

interview questions js[ Anurag Singh ProCodrr]

reactnative_creation