2016-08-01 2 views
0

안녕하세요 저는 react/Redx 개발 환경을 처음 사용하기 때문에 어떤 도움을 주셔서 감사합니다. 나는이 API를 만들기 위해 노력하고있어모든 내부 약속이 해결되기 전에 해결 된 약속, 여러 개의 비동기 API 호출

내가 this post에서 제안 된 방법을 찾아 내 app.js에 dispatch(fetchAllPositions(selectedUserDivision))를 호출하여 비동기 때 componentDidMount 호출하고 fetchAllPositions 기능은 Promise.all

에 두 개의 액션 기능을 래핑
export function fetchAllPositions(division) { 
    return dispatch => Promise.all([ 
     dispatch(fetchUserPositionsIfNeeded(division)), 
     dispatch(fetchDefaultPositionsIfNeeded(division)) 
    ]) 
    .then(console.log("fetched both")) 
} 

두 가지 액션 함수는 거의 동일하며 약간 다른 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이 부울을 반환 단지 순수 함수 곳은, 다음과 같이 그 중 하나가 보인다 데이터가 새 상태로 업데이트 될 준비가되면 전송됩니다.

문제는이 2 RECEIVE에 대기하지 마쳤 처음 2 REQUEST.then 않는 것, 모든 REQUEST1REQUEST2RECEIVE1RECEIVE2는 지금 .then(console.log("fetched both"))

을하기 전에 와서에 대한 Promise.all 대기해야하는 것입니다 와서.

enter image description here

을 나는 그것이 requestUserPositions()의 중첩 된 성격 용의자 REQUEST 작업은 간단한 기능이며, 감속기에 그냥 넘겼 fetch()

래핑 함수 내에서 isFetching 재산 true에 :

function requestUserPositions(division) { 
    return { 
    type: REQUEST_USER_POSITIONS, 
    division 
    } 
} 

이 긴 문제에 대한 미안하지만 난 어떤 제안을 감사하겠습니다 .

답변

-1

.then()는 디스패치 내부 래핑 때 Promise.all()가 실행되는대로 동시에 실행 도착 것으로 밝혀에게 부주의 실수이다.

의도 파견 순서

ComponentDidMount

에서 수행 한 최종 디스패치 dispatch(fetchAllPositions(selectedUserDivision))then(()=>console.log 태그에 의해 생성 된