私は単純なCRUDアプリケーションを構築しようとしています。ユーザーはプロフィールページを持っており、自分のフィールドを記入して保存することができます。私は(Reduxのアプリケーションで)HTTP要求のaxiosを使用してい約束、エラーが発生した場合は 'then'を発しません。
:
export const createCard = (data) => (dispatch) =>
axios.post('/api/cards', data)
.then(
response => dispatch({
type: actions.CREATE_CARD_SUCCESS,
card: response.data
})
error => dispatch({
type: actions.CREATE_CARD_FAILURE,
message: error.message
})
)
.then(() => /* do something here, if no error occur */);
し、要求が正常に終了した後、私は、この方法で何かをしたい私の質問です。
export const createCard = (data) => (dispatch) =>
axios.post('/api/cards', data)
.then(
response => {
dispatch({
type: actions.CREATE_CARD_SUCCESS,
card: response.data
});
},
error => {
dispatch({
type: actions.CREATE_CARD_FAILURE,
message: error.message
});
return error;
}
)
.then(error => {
if (!error) {
/* do something here, if no error occur */
}
});
しかし、それはあまりにも多くの定型ですが、それはエラーがこれは、このように達成することができ
を発生している場合でも、常に起動します。
この場合、Jquery.ajaxが便利です。
$.ajax()
.done()
.fail()
.always()
.then(() => { /** this callback will be fired if no error occur **/})
この動作をAxiosでシミュレートするにはどうしたらよいですか?
ok、私の知るように、jqueryのようなロジックを作る方法はありません。私ができることはエラーを投げることだけです。あなたのお返事の両方にお返事ありがとうございます。<3 –
@SmikeNelipov - 「jQueryのような論理を作る」とはどういう意味ですか? – jfriend00
jqueryは実行しません.failの後 '$ .ajax(... options).done()。fail()。then(。*コールバックがエラーをキャプチャした場合、このコールバックは起動しません。 /) ' –