redux 有没有可能等发货结束

bbmckpt7  于 2023-02-16  发布在  其他
关注(0)|答案(3)|浏览(110)

我有2个动作,我从第一个动作调用到第二个动作....我需要等到第二个动作完成,然后才继续这个动作。

// first action
export const getDataFromAdmin = () => {
    return async (dispatch, getState) => {
        dispatch(getDeviceLocation());
        console.log('only after getDeviceLocation is finsih');
        AdminRepository.getDataFromAdminAndEnums(dispatch)
            .then(adminData => {
              //some code 
            })
            .catch(error => {
                console.log(`Splash Error = ${error.message}`);
            });
    };
};

//second action
export const getDeviceLocation = () => {
    return async dispatch => {
        dispatch({ type: actionsType.GET_DEVICE_LOCATION });
        LocationManager.getCurrentPosition()
            .then(location => {
         
                dispatch({ type: actionsType.GET_DEVICE_LOCATION_SUCCESS });
            })
            .catch(error => {
                dispatch({ type: actionsType.GET_DEVICE_LOCATION_ERROR, message: error.message });
            });
    };
};
r7s23pms

r7s23pms1#

不,这是不可能的,因为分派一个动作就像触发动作,动作只需要调用中间件或者reducer,就是这样,动作不会等待reducer或者中间件的完成,它只是调用并完成它的工作。

hrysbysz

hrysbysz2#

好的,最后我把async wait by pass dispatch作为函数的一个参数。

nxagd54h

nxagd54h3#

    • 是的,您可以使用await dispatch,但前提是您使用的是redux-thunk中间件。**

dispatch是默认的同步操作。redux-thunk中间件允许您分派作为dispatch函数的操作,如您问题中的操作,除了标准的{ type: 'SOME_NAME' }操作对象之外,redux-thunk还使这些"thunk"操作的调度是异步的。这允许您使用aysnc/调用dispatch(myThunkAction());时使用awaitPromise.then()

async function someFunction() {
    await dispatch(myThunkAction());
    doSomethingElse();
}
dispatch(myThunkAction()).then(() => {
    doSomethingElse();
});

下面是redux-thunk文档中的一个示例:

// In fact I can write action creators that dispatch
// actions and async actions from other action creators,
// and I can build my control flow with Promises.

function makeSandwichesForEverybody() {
  return function (dispatch, getState) {
    if (!getState().sandwiches.isShopOpen) {
      // You don’t have to return Promises, but it’s a handy convention
      // so the caller can always call .then() on async dispatch result.

      return Promise.resolve()
    }

    // We can dispatch both plain object actions and other thunks,
    // which lets us compose the asynchronous actions in a single flow.

    return dispatch(makeASandwichWithSecretSauce('My Grandma'))
      .then(() =>
        Promise.all([
          dispatch(makeASandwichWithSecretSauce('Me')),
          dispatch(makeASandwichWithSecretSauce('My wife'))
        ])
      )
      .then(() => dispatch(makeASandwichWithSecretSauce('Our kids')))
      .then(() =>
        dispatch(
          getState().myMoney > 42
            ? withdrawMoney(42)
            : apologize('Me', 'The Sandwich Shop')
        )
      )
  }
}

您可以在redux-thunk docs的Composition部分查看完整的代码,了解它们是如何定义makeASandwichWithSecretSauce thunk的。
Promise的解析值将是您在myThunkAction函数中return的值。通常thunk会调用dispatch,而不会调用任何return,因此很少使用此功能。
内部函数的任何返回值都可以作为dispatch本身的返回值。这便于编排异步控制流,其中thunk操作创建者相互分派,并返回Promises以等待对方完成。
一个三个三个一个

相关问题