Promise调用方式

//第一种调用方式
new Promise((resolve,reject)=>{
  setTimeout(function() {
    console.log('模拟异步请求操作!');
    //resolve('成功时主动调用这个函数,会执行下面的then方法')
   // reject('失败时主动调用这个函数,会执行下面的catch方法')
  },1000);
}).then(data=>{
  //成功后的处理代码
  console.log(data);
  //这里执行完毕后可以继续new Promise进行链式调用
  new Promise((resolve,reject)=>{}).then();
}).catch(err=>{
  //失败后的处理代码
  console.log(err);
})
//第二种写法
new Promise((resolve,reject)=>{
  setTimeout(function() {
    console.log('模拟异步请求操作!');
    //resolve('成功时主动调用这个函数,会执行下面的then方法')
   // reject('失败时主动调用这个函数,会执行下面的catch方法')
  },1000);
}).then(data=>{
  //成功后的处理代码
  console.log(data);
  //这里执行完毕后可以继续new Promise进行链式调用
  new Promise((resolve,reject)=>{}).then();
},err=>{
  //失败后的处理代码
  console.log(err);
})
原文地址:https://www.cnblogs.com/jiangyunfeng/p/13401871.html