promise的基本用法

1、pormise基本用法
let p = new Promise(function(resolve, reject) //异步操作
   setTimeout(function(){
     //resolve成功的回调,reject为失败的回调; resolve(data); },2000); })

2、then第一个参数执行resolve,第二个参数执行reject;
p.then(res=>{
  console.log(res)
}).catch(err=>{
  //catch执行reject类似then的第二个参数,如果执行resolve回调异常时也会进入;
  console.log(err)
})

3、并行执行多个异步
Promise.all([Async_1,Async_2,Async_3]).then(res=>{
  //res为一个数组接收多个异步的回调参数
  console.log(res)
)

4、比较执行多个异步(应用场景:异步请求超时后的处理等)
Promise.race([Async_1,Async_2,Async_3]).then(res=>{
  //res为先执行完的异步操作的回调
  console.log(res)
}
)




posted @ 2018-10-16 14:27  大望叫我来巡山  阅读(213)  评论(0)    收藏  举报