promise 承诺 (个人笔记)

promise().then(f1).catch(f2).then(f3).done(ss)

(1)     var promise = new Promise(function(resolve, reject) {
 // ... some code
 if (/* 异步操作成功 */){
 resolve(value);
 } else {
 reject(error);
 }
});
(2) promise.then(function(value) {
 // success
}, function(error) {
 // 

(3)

created(){
   let names = 'xia'
   console.log('名称是',Promise.resolve(names))


    Promise.resolve('foo')   //直接生成一个promise  对象
    // 等价于
    new Promise(resolve => resolve('foo'))
    var p = Promise.all([11, 22, 33])
    console.log('pss',p)
   this.timeout(2000).then((value)=>{
       console.log('方法',value)
   })


   let conData = this.testM(1500)
   conData.then(function(value){
       console.log('你好',value)
   },function(error){
       console.log('一不小心就失败了')
   })
  },


testM(ms){
        let _this = this
        var promise = new Promise(function(reslove,reject){

            let data = _this.hah()
            if(data == 2){
                setTimeout(reslove, ms,data);
                // setTimeout(() => {
                    // console.log(222)
                    //   reslove(data)
                // }, 1200);
               
            }else{
                console.log(111)
                 reject(error)
            }
          
        })
          return promise
    }, 
    hah(){
        return 1>2? 1:2
    },
   timeout(ms) {
        return new Promise((resolve, reject) => {
            setTimeout(resolve, ms, 'done');
        })
    },

 

你可能感兴趣的:(promise 承诺 (个人笔记))