ES6的Promise实例

function p1(x){
      return new Promise(function(resolved,rejected){
          if(x==1){
            console.log(1)
            resolved(x)
          }else{
              rejected(2)
          }
      })
}
function p2(){
      return new Promise(function(resolved,rejected){
          console.log(2)
          resolved(2)
      })
}
p1(2).then(function(data){
      console.log(data)
      p2();
}).catch(function(err){
      console.log(err)
});



function p1(x){
       return new Promise(function(resolved,rejected){
                  if(x==1){
                    console.log(1)
                    resolved(x)
                  }else{
                      rejected("1出错了")
                  }
              })
}
function p2(x){
              return new Promise(function(resolved,rejected){
                  if(x==2){
                    console.log(1)
                    resolved(x)
                  }else{
                      rejected("2出错了")
                  }
              })
}
function p3(x){
              return new Promise(function(resolved,rejected){
                  if(x==3){
                    console.log(1)
                    resolved(x)
                  }else{
                      rejected("3出错了")
                  }
              })
}
          
Promise.all([p1(1),p2(2),p3(3)]).then((result)=>{console.log(result)}).catch((err)=>{console.log(err)});

 

posted on 2018-07-03 14:48  emmeet  阅读(90)  评论(0编辑  收藏  举报

导航