【发布时间】:2019-08-01 01:13:56
【问题描述】:
在 react-native 中编写应用程序时,我遇到了与 Promise 相关的某些障碍,所以我有一个负责授权请求的函数
export const authorizeRequest = async () => {
const token = await deviceStorage.getItem('accessToken');
return axios.create({
timeout: 2000,
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
}
});
};
为了从中获取数据,我以风格编写代码
authorizeRequest().then(a => a.get('http://192.168.0.60:8080/users/echo2/asd')
.then(response => ToastAndroid.show('Response ' + response.data, ToastAndroid.SHORT))
.catch(error => ToastAndroid.show('error ' + JSON.stringify(error), ToastAndroid.LONG)))
是否可以在调用authorizeRequest().then(....)时避免第一次使用.then,使查询看起来像authorizeRequest().get('xxx').then(xxx).catch(xxx)
谢谢!
【问题讨论】:
标签: react-native promise axios