简体   繁体   中英

how can i use AsyncStorage data Everywhere?

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),
      ]);
    }

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) {
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM