promise 승낙 (개인 노트)

1547 단어
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');
        })
    },

좋은 웹페이지 즐겨찾기