业务中遇到的问题:
第二个方法的参数需要用到第一个请求后获取的值
旧的解决方式
const getResult = ()=>{
axios.get(url1).then(res=>{
axios.get(`${url2}?id=${res.id}`).then(result=>{
console.log('result', result)
})
})
}
使用async/await方式
const getId = ()=>{
return axios.get(url1).then(res=>{
return res.id
})
}
const getResult = async()=>{
const id = await getId()
axios.get(`${url2}?id=${id}`).then(result=>{
console.log('result', result)
})
}