目前想到得使用场景:当我有两个函数,其中有一个是异步的,但是必须要先执行的时候就可以使用async和await了(当时我是用了定时器,这是错误的解决方案)
使用方法:
fun1 (num1, num2) {
let p = new Promise(va => {
setTimeout(() => {
let num3 = num1 + num2;
va(num3);
}, 3000)
})
return p
},
fun2 (num3) {
let p = new Promise(va => {
setTimeout(() => {
let num4 = num3 * 10;
va(num4);
}, 1000)
})
return p
},
fun3 (num4) {
console.log('最终结果:' + num4)
},
//触发函数
async fun4 () {
let _this = this;
let a = await _this.fun1(1, 2)
console.log('第一个执行完了' + a)
let b = await _this.fun2(a)
console.log('第二个执行完了' + a)
let c = await _this.fun3(b)
console.log('第三个执行完了' + a)
}
使用简述:
需要先执行的函数:A ,需要后执行的函数: B ,触发函数: C
c前加async修饰,在调用a,b的时候,a,b前加await
感觉不太对,先这样吧!!!!!!!!!!!!!!!!!!!!