こんにちは私はreact/redux開発環境を初めて使いました。私のapp.jsでdispatch(fetchAllPositions(selectedUserDivision))
すべての内部の約束が解決される前に、約束された解決策、複数の非同期API呼び出し
を呼び出すことによってcomponentDidMount
は私がthis postで提案された方法を発見し、fetchAllPositions
機能がPromise.all
私は2 APIを作るしようとしているが、非同期呼び出し、
export function fetchAllPositions(division) {
return dispatch => Promise.all([
dispatch(fetchUserPositionsIfNeeded(division)),
dispatch(fetchDefaultPositionsIfNeeded(division))
])
.then(console.log("fetched both"))
}
2つのアクション関数はほぼ同じです。若干異なるAPI URLを呼び出します。
がfunction fetchUserPositions(division) {
return dispatch => {
const url = apiOptions.server + `/api/user/position?division=${division}`
dispatch(requestUserPositions(division))
return fetch(url, options)
.then(response => response.json())
.then(json => dispatch(receiveUserPositions(division,json)),
err => console.log(err))
}
}
export function fetchUserPositionsIfNeeded(division) {
return (dispatch, getState) => {
if (shouldFetchUserPositions(getState(), division)) {
return dispatch(fetchUserPositions(division))
} else {
return Promise.resolve()
}
}
}
ロジックはデータを引きながら、更新のために、REQUEST...
アクションが送信されることで、その後、RECEIVE...
アクションは次のとおりです。shouldFetchUserPosition
はブール値を返すだけの純粋な関数であり、次のようにそれらの一つが見えますデータが新しい状態に更新される準備が整ったときに送信されます。
トラブルがPromise.all
はすべて今.then(console.log("fetched both"))
を行う前に来るにREQUEST1
REQUEST2
RECEIVE1
RECEIVE2
を待つ必要があるということです、それは最初の2 REQUEST
後.then
は2 RECEIVE
のを待っていない、完成しているんですで来る。
私はそれがrequestUserPositions()
のネストされた自然の疑いREQUEST
アクションは、単純な関数であり、減速機に、それはちょうど反転するfetch()
ラップ関数内isFetching
プロパティtrue
へ:
function requestUserPositions(division) {
return {
type: REQUEST_USER_POSITIONS,
division
}
}
この長い問題のため申し訳ありませんが、私は任意の提案をいただければと思います。