async function testResult() {
let first = await doubleAfter2seconds(30);
let second = await doubleAfter2seconds(50);
let third = await doubleAfter2seconds(30);
console.log(first + second + third);
}
function doubleAfter2seconds(num) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(2 * num)
}, 2000);
} )
}
function a(){
console.log(1)
}
a()
testResult()
console.log('success')
百度后看到这么一段代码
async是将方法异步处理
await是等待方法执行成功后再执行下面的代码,相当于抵消了async的作用?
(这两个是这么理解吗)
我今天看到个这种写法,这种有什么作用呢?求大佬解惑
await this.getConfig()
async getConfig() {
var res = await this.$get('server/paotui/getconfigbyuid')
if (res.errcode != 0) {
this.$toast(res.errmsg);
return;
}
this.feeOptions = res.config.xiaofei
},
async是标识这是个异步函数,在此函数内部可使用await,await顾名思义就是等待,等待await后面的代码执行,比如等待一个请求,等待期间程序可以做其他事,也就相当于在此处中断退出,当请求返回后,引擎会重新执行await下面的的代码。
https://www.bookstack.cn/read...
https://developer.mozilla.org...