Friday, 8 December 2017

pomise tutorial


https://scotch.io/tutorials/javascript-promises-for-dummies
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261
/* ES5 */
var isMomHappy = true;

// Promise
var willIGetNewPhone = new Promise(
    function (resolve, reject) {
      let data=new Promise(function(resolve,reject){

        let i;
      for(i=0;i<100;i++)
      {
        console.log(i);
      }

    if(i==100)
    {
      resolve(isMomHappy);
    }
  }).then(function(isMomHappy){
        if (isMomHappy) {
            var phone = {
                brand: 'Samsung',
                color: 'black'
            };
            resolve(phone); // fulfilled
            //console.log(phone);
        } else {
            var reason =new Error('mom is not happy');
            reject(reason); // reject
        }
      }).catch(function(e){

        console.log(e);
      });

}).then(function(phone){
  console.log(phone);
}).
catch((reason)=>{
  console.log(reason);
});

No comments:

Post a Comment