2017-03-06 39 views
0

我們的應用程序使用ATTEMPT - SUCCESS - FAILURE方法處理來自服務器的響應。只有當一個不同的傳奇完成時,你能否稱爲傳奇並繼續執行?

我有一個需要這樣的表現發電機功能:

function * getSingleSectorAttempt(action) { 
    const sectors = yield select(getSectors); 
    if (!sectors) { 
    //If there are no sectors, I need to call the GET_SECTORS_ATTEMPT action 
    //and only restart/continue this saga when the GET_SECTORS_SUCCESS action is fired 
    } 
    const id = sectors[action.name].id; 
    try { 
    const response = yield call(api.getSector, id); 
    //... 
    } catch (err) { 
    //... 
    } 
} 

從我讀過的終極版佐賀的文檔,這似乎並沒有立即成爲可能。但是,我想看看我是否錯過了一些東西。在if(!sectors)條件塊

yield fork(takeLatest, Type.GET_SECTORS_SUCCESS, getSingleSectorAttempt); 
yield put(Actions.getSectorsAttempt()); 

,但在這工作不堅持最初的GET_SINGLE_SECTOR_ATTEMPT動作參數,我不知道如何得到它這樣做沒有進入回調:我已經試過這和論據意大利麪條。

回答

1

讓你等待被分派的作用效果是take。在你的情況下:

function* getSingleSectorAttempt(action) { 
    let sectors = yield select(getSectors); 
    if (!sectors) { 
    yield put(getSectorsAttempt()); 
    yield take(GET_SECTORS_SUCCESS); 
    sectors = yield select(getSectors); 
    } 
    // resume here as normal 
} 

你自己的答案可能會有意想不到的副作用。例如,如果getSectors可以在應用程序的生命週期中多次返回一個falsy值,那麼您將有幾個分叉進程等待GET_SECTORS_SUCCESS發送,並且每個執行副作用,每個副作用都保持對觸發它的操作的引用。

0

哎呀,想通了:

function* getSingleSectorAttempt(action) { 
    const sectors = yield select(getSectors); 
    if(!sectors){ 
     //Pass initial action in a callback function like so: 
     yield fork(takeLatest, Type.GET_SECTORS_SUCCESS,() => getSingleSectorAttempt(action)); 
     yield put(Actions.getSectorsAttempt()); 
    } else { 
     const id = sectors[action.name].id; 
     try { 
     const response = yield call(api.getSector, id); 
     //... 
     } catch (err) { 
     //.. 
     } 
    } 
    }