我想知道我該如何退還承諾表格Axios?我不確定是否需要使用攔截器?迴歸Axios的承諾?
我有這樣的代碼,現在
export function fetchStorage() {
return function (dispatch) {
return new Promise(function(resolve, reject) {
if (1 === 1) {
resolve('it works!');
} else {
reject(':(');
}
});
};
}
和
this.props.fetchStorage().then(function() {
console.log('then');
});
現在說我想改變fetchStorage通過AJAX做一些事情,我會像
var instance = axios.create({
baseURL: 'http://localhost:54690/api',
timeout: 2000,
});
instance.get('/Storage/Get')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
我該如何回覆承諾,而不是在這裏做?
編輯
只是爲了澄清我爲什麼這樣做,我有這樣的事情
componentWillMount() {
this.props.setPreLoader(true);
this.props.fetchStorage().then(function() {
this.props.setPreLoader(false);
});
}
export function fetchStorage() {
return function (dispatch) {
return new Promise(function (resolve, reject) {
axiosInstant.get('/Storage/Get')
.then(function (response) {
let payload = response.data;
console.log(payload);
dispatch({ type: actions.FETCH_STORAGE, payload: { storages: payload } });
})
.catch(function (error) {
console.log(error);
});
});
};
}
爲什麼不'回instance.get('/存儲/得到')'我在這裏錯過了什麼? –
以及我正在使用redux模式,所以我需要發送調度,如果我只是像你一樣返回,那麼我總是必須記得每次使用該函數時發送該調度。 – chobo2
@ chobo2我不明白你爲什麼沒有回覆axios已經給你的承諾。 –