猿问

在线等,挺急的!axios怎么实现同步机制感激不尽

asyncfunctiongetList(){
returnawaitaxios({
method:'post',
url:'/list/tododata/',
data:json
}).then(res=>{
returnaxios.get('/list/tododata',{
params:{
'page':newState.current,
}
}).then((res)=>{
constdata=res.data;
returndata.results//想获取这个东西
}).catch((error)=>{
console.log(error);
});
}).catch((err)=>{
console.log(err);
});
}
getList().then(value=>{
newState.list=value
})
returnnewState
async返回的还是一个Promise,获取Promise的值还是异步的,以至于最后return的newSate还是没有被更新,请问想要获取最新的newState该怎么改?
有只小跳蛙
浏览 409回答 2
2回答

温温酱

asyncfunctiongetList(){awaitaxios({method:'post',url:'/list/tododata/',data:json}).catch((error)=>{console.log(error);});constresults=awaitaxios.get('/list/tododata',{params:{'page':newState.current,}}).then((res)=>{constdata=res.data;returndata.results;}).catch((error)=>{console.log(error);});returnresults;}

吃鸡游戏

functionpostTododata(){returnnewPromise((resolve,reject)=>{axios({method:'post',url:'/list/tododata/',data:json}).then(res=>{resolve(res.data);})})}functiongetTododata(){returnnewPromise((resolve,reject)=>{axios({method:'get',url:'/list/tododata/',params:{'page':newState.current,}}).then(res=>{resolve(res.data);})})}asyncfunctiongetList(){awaitpostTododata();newState.list=awaitgetTododata();}getList();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答