es6 Promise 对象、.then()
<script type="text/javascript"> let promise=new Promise((resolve,reject)=>{ console.log(‘promise初始化状态:pending ‘); // 同步执行 setTimeout(()=>{ console.log(‘开启定时器,模拟异步执行‘) // resolve(‘异步返回,执行成功‘) // 状态改成fulfilled reject(‘异步返回,执行失败‘) // 状态改成rejected },1000) }); promise .then((data)=>{ // 第一个回调 成功resolve console.log(‘resolve‘) console.log(data) },(data)=>{ // 第二个回调 成功reject console.log(‘reject‘) console.log(data) }) console.log(‘代码继续执行‘) </script>
<script type="text/javascript"> let promise=new Promise((resolve,reject)=>{ console.log(‘promise初始化状态:pending ‘); // 同步执行 $.get(‘./data.json‘,{},function(result){ console.log(‘ajax异步操作‘); if(result.ret){ console.log(‘执行OK‘) resolve(result.data); }else{ console.log(‘执行失败‘) reject(result.data) } },‘json‘); }); promise .then((data)=>{ // 第一个回调 成功resolve console.log(‘resolve‘) console.log(data) },(data)=>{ // 第二个回调 成功reject console.log(‘reject‘) console.log(data.errorInfo) }) console.log(‘代码继续执行‘) </script>
相关推荐
前端开发Kingcean 2020-07-30
88520191 2020-06-13
Magicsoftware 2020-05-26
nmgxzm00 2020-11-10
xixixi 2020-11-11
88254251 2020-11-01
MarukoMa 2020-09-02
88234852 2020-09-15
陈旭阳 2020-08-31
whynotgonow 2020-08-19
whynotgonow 2020-07-29
bowean 2020-07-08
前端开发Kingcean 2020-07-08
88520191 2020-07-05
前端开发Kingcean 2020-06-27
88481456 2020-06-18
whynotgonow 2020-06-16
88520191 2020-06-13
89500297 2020-06-13