0

I am using the saga library. And tokens are stored in AsyncStorage. What I want is to freely use the token obtained from AsyncStorage in the loadUserPosts function or in loadPosts. In this case, where should async be added and how do I fix the code?

this is my code

    const token = await AsyncStorage.getItem('tokenstore');

    function* loadUserPosts(action) {
      try {
        console.log(token)
        yield put({
          type: LOAD_USER_POSTS_SUCCESS,
          data: result.data,
        });
      } catch (err) {
      }
    }

    function* loadPosts(action) {
      try {
        console.log(token)
        yield put({
          type: LOAD_POSTS_SUCCESS,
          data: result.data,
        });
      } catch (err) {
    }




    function* watchLoadPost() {
      yield takeLatest(LOAD_POST_REQUEST, loadPosts);
    }

    function* watchLoadUserPosts() {
      yield throttle(5000, LOAD_USER_POSTS_REQUEST, loadUserPosts);
    }

    export default function* postSaga() {
      yield all([

        fork(watchLoadPosts),
        fork(watchLoadUserPosts),
      ]);
    }

1 Answer 1

1

You can try and yield you async result. You might not even need async becuase generator function will yield until it gets a result.

function* loadUserPosts(action) {
  try {
    const token = yield AsyncStorage.getItem('tokenstore');
    console.log(token)
    yield put(LOAD_USER_POSTS_SUCCESS(token));
  } catch (err) {
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.