【发布时间】:2017-04-14 00:44:52
【问题描述】:
我正在尝试使用 es6 promise ,依次进行两个远程调用, 这是我的代码
recordsCount(){
let classInstance=this;
let stateIns=this.state;
return axios.post('/api/projectDocs/count',stateIns.gpSearch).then((response)=>{
stateIns.totalRecords=response.data;
classInstance.setState(stateIns);
});
}
loadGpDocs(start, end){
let classInstance=this;
let stateIns=this.state;
stateIns.gpSearch.start=start;
stateIns.gpSearch.end=end;
return axios.post('/api/projectDocs/search',stateIns.gpSearch).then((response)=>{
stateIns.data.gpDocs=response.data;
classInstance.setState(stateIns);
});
}
调用这两个函数的代码
classInstance.recordsCount().then(classInstance.loadGpDocs(0, 20).then(function () {
stateIns.ready = true;
classInstance.setState(stateIns);
}));
首先调用记录计数,这会返回一个 axios promise ,然后加载数据,这个返回 axios promise 然后将更改应用到 UI。
我遗漏了一些东西,调用不按顺序,请帮助我理解promise,为什么这段代码不按顺序?
【问题讨论】:
标签: javascript reactjs es6-promise axios