异步
export async function fun1 () { res = await test.post(link, obj) res.data.data = JSON.parse(decrypt(res.data.data, password)) return res }
说明:await 让js进行等待 直到promise 执行并返回结果
import { fun1 } from '../../util/...js'
fun1().then(data => {
Console.log('')
}).catch(err => {
Console.log('')
})
说明:async代表异步的,用来表示一个异步的函数,返回一个promise----->可以使用then方法添加回调,catch抛出异常
async function foo({ username, password }) { const { userId } = await ajax('login', { username, password }) const { token } = await ajax('getToken', { userId }) const res = await ajax('getOtherInfo', { token }) // do something ... } ajax('login', { username, password }) .then(({ userId }) => ajax('getToken', { userId })) .then(({ token }) => ajax('getOtherInfo', { token })) .then(res => { // do something... })
async/await的优势就在于处理then式调用链

浙公网安备 33010602011771号