react多异步串联方法

通过使用redux-saga 

1.原理  通过使用 generator 替代promise(promise.all也可以)实现

比如项目中,遇到先获取工点pointId,在获取标段sectionId

 

function* getPointId(userId){
        const data = yield call('api/getPointId',{userId})
        yield put({
            type:'Get_POINT_SUCCESS',
            payload:data,
        })
    }
    function* getSectionId(pointId){
        const data = yield call('api/getSectionId',{pointId})
        yield put({
            type:'Get_SECTION_SUCCESS',
            payload:data,
        })
    }

    function initLoadData(userId){
        yield getPointId(userId);
        yield getSectionId(getPointState(state));
        yield put({
            type:'Get_DATA_SUCCESS'
        })
    }
posted @ 2022-03-23 10:26  天堂花草  阅读(109)  评论(0)    收藏  举报