将返回的promise 对象改为 async/await写法
2022-02-17 本文已影响0人
想溜了的蜗牛
示例代码:
// 封装请求函数
const request = (url, params) => {
return new Promise((resolve, reject) => {
// ...do something
})
}
// 使用时
const handleLogin = () => {
request(
'/basic/login',
{
usename: 'sunshine',
password: '123456'
}
).then(res => {
// success do something
}).catch(err => {
// fail do something
})
}
上面的代码使用async/await的话可以改写成:
const handleLogin = async () => {
const res = await request('/basic/login', {
usename: 'sunshine',
password: '123456'
})
const info = await request('/basic/getuserinfo', {
id: res.id
})
this.userInfo = info
}