我如何在 redux-saga 中使用 yield?

How can i use yield in redux-saga?

收到refresh函数的结果值后,axiosInstace在将accesstoken保存到AsyncStorage之前执行,所以更新后的token在axios.js到AsyncStorage.getItem中是取不到的。我想先在刷新中保存 accesstoken 并在 axios.js 中获取 acesstoken 并发送到 axiosInstace

我该如何解决这个问题?

这是我的代码

(saga.js)

    function getPostAPI(data) {
      return axiosInstace.post('/kakao/getpost', data);
    }


    function* getPost(action) {
      try {
        const result = yield call(getPostAPI, action.data);
        yield put({
          type: GETPOST_SUCCESS,
          data: result.data,
        });
      } catch (err) {
        if (err.response.data === 'jwtEx') {
          yield put({
            type: REFRESH_REQUEST,
            // data: action.data,
          });
          yield put({
            type: GETPOST_REQUEST,
            data: action.data,
          });
        } else {
          yield put({
            type: GETPOST_FAILURE,
            error: err.response.data,
          });
        }
      }
    }


    function refreshAPI() {
      return axiosInstace.post('/kakao/refresh');
    }

    function* refresh() {
      try {
        const result = yield call(refreshAPI);
        yield AsyncStorage.setItem(
          'accesstoken',
          `${result.data.accessToken}`,
          () => {
            // console.log('accesstoken 재발급 저장 완료');
          },
        );
        yield put({
          type: REFRESH_SUCCESS,
          data: result.data,
        });
      } catch (err) {
        yield put({
          type: REFRESH_FAILURE,
          error: err.response.data,
        });
      }
    }

(axios.js)

    AxiosInstance.interceptors.request.use(async (cfg) => {

      const acecesstoken = await AsyncStorage.getItem('accesstoken');
      const refreshtoken = await AsyncStorage.getItem('refreshtoken');

      if (acecesstoken) {
        cfg.headers.Authorization = `Bearer ${acecesstoken}    ${refreshtoken}`;
      }

      return cfg;
    });

    export default AxiosInstance;

一个简单的解决方案是直接调用您的 refresh() 生成器:

function* getPost(action) {
      try {
        const result = yield call(getPostAPI, action.data);
        yield put({
          type: GETPOST_SUCCESS,
          data: result.data,
        });
      } catch (err) {
        if (err.response.data === 'jwtEx') {
          yield call(refresh);
          
          // you could also redispatch the original action
          yield put(action);
        } else {
          yield put({
            type: GETPOST_FAILURE,
            error: err.response.data,
          });
        }
      }
    }

或者您可以在 REFRESH_SUCCESSREFRESH_FAILURE 之间开始比赛:

const { success, failure } = yield race({
    success: take('REFRESH_SUCCESS'),
    failure: take('REFRESH_FAILURE'),
  });

if(success) {
  // continue
} else {
  // handle refresh failure
}