博客园站长
这是人类成长进步中记录的每一时刻

 

1、引用此js,没有自行百度

 import regeneratorRuntime from "sudu8_page/resource/js/runtime.js";

 

2、js代码:

//一、同步执行: async await 关键词

调用方也得加 async

 

async testAsync() {
const result = await this.testPromise()
console.log('async test--', result)    
},

testPromise() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log('Promise handle')
          resolve(1234)
        }, 2000)
      })
    },  
 
打印输出

Promise handle  
async test-- 1234 

//二、简单的

testPromise() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log('Promise handle')
          resolve(1234)  //提前返回回去,表示正确数据 then res
          //reject(33333)  //提前返回回去,表示异常内容 catch  err
        }, 2000)
      })
    },  
 
a:function(){
this.testPromise().then(res=>{
console.log(res)
}).catch(function(err){
console.log(err)  //如果有 reject 则打印这个的值
})
}

打印:

1234

or

33333

   

 
posted on 2020-08-19 15:41  dm3344  阅读(1187)  评论(0编辑  收藏  举报