Sunday, 7 January 2018

What is Promises in Jquery



exp-

const money = 600;

const buyCar = new Promise(
 
  function(resolve,reject){
 
    if(money > 500){
      const car={
        Name:"Maruti",
        Year: "2015",
        Color:"White"
      }
      setTimeout(function(){
         resolve(car);
      },3000)
    }
    else{
      reject('Rejected due to not enogh amount');
    }
  }
)

//Handle multiple promises

const buyMultipleCars= [buyCar,buyCar,buyCar,buyCar,buyCar]
const cars = Promise.all(buyMultipleCars);
cars.
 then(function(data){
        console.log('success=>',data)
      }).
     catch(function(error){
       
         console.log('fail=>',error)
      });
     
//console.log(buyMultipleCars);

buyCar.
      then(function(data){
       console.log('success=>',data)
      }).
      catch(function(error){
        console.log('fail=>',error)
      });
        

No comments:

Post a Comment