使用Promise 实现刷新页面、业务代码的先后执行
new Promise((resolve => {
this.setState({
key:value
})
resolve();
})).then(()=>{
try {
this.refs['panel'].handleSearch();
} catch (error) {
console.error(error.message)
}
});
自定义封装(方便调用)
/**
*
* @param service
* @param then
*/
requestService = (config) => {
new Promise((resolve) => {
resolve(config.request());
}).then((response) => {
config.then(response)
});
}
自定义调用
this.requestService({
request: () => {
this.setLoading(true);
let result = requestInterface()
return result
},
then: (result) => {
this.setLoading(false);
this.handleResponse(result);
this.refreshData();
}
});