繁体   English   中英

在AsyncStorage(getItem)中返回键的值,而不是Promise

[英]Returning value of the key in AsyncStorage (getItem) instead of a promise

我想在另一个函数中使用AsyncStorage中存储的值。

这是我的ValueHandler.js

import { AsyncStorage } from 'react-native';

export async function getSavedValue() {
  try {
    const val = await AsyncStorage.getItem('@MyStore:savedValue');
    console.log("#getSavedValue", val);
    return val;
  } catch (error) {
    console.log("#getSavedValue", error);
  }
  return null;
}

我做了上面的函数getSavedValue()以获取存储在AsyncStorage中的saveValue的值。

import {getSavedValue} from '../lib/ValueHandler';
import Api from '../lib/Api';

export function fetchData() {
  return(dispatch, getState) => {
    var params = [
      'client_id=1234567890',
    ];
    console.log("#getSavedValue()", getSavedValue());
    if(getSavedValue() != null) {
      params = [
        ...params,
        'savedValue='+getSavedValue()
      ];
    }
    params = params.join('&');
    console.log(params);
    return Api.get(`/posts/5132?${params}`).then(resp => {
      console.log(resp.posts);
      dispatch(setFetchedPosts({ post: resp.posts }));
    }).catch( (ex) => {
      console.log(ex);
    });
  }
}

我该如何实现? 我真的想要一种简单的方法来将值保存在本地存储中,并仅在需要进行API调用时才检索它。

有人对此有任何建议或解决方案吗?

您可以执行以下操作:

import { AsyncStorage } from 'react-native';

export async function getSavedValue() {
    return new Promise ((resolve, reject) => { 
      try {
        const val = await AsyncStorage.getItem('@MyStore:savedValue');
        console.log("#getSavedValue", val);
        resolve(val);
      }catch (error) {
        console.log("#getSavedValue", error);
        reject(error)
      }  
   })
}


import {getSavedValue} from '../lib/ValueHandler';
import Api from '../lib/Api';

export function fetchData() {
  return async (dispatch, getState) => {
    var params = [
      'client_id=1234567890',
    ];
    console.log("#getSavedValue()", getSavedValue());
    if(getSavedValue() != null) {
      params = [
        ...params,
        'savedValue='+ await getSavedValue()
      ];
    }
    params = params.join('&');
    console.log(params);
    return Api.get(`/posts/5132?${params}`).then(resp => {
      console.log(resp.posts);
      dispatch(setFetchedPosts({ post: resp.posts }));
    }).catch( (ex) => {
      console.log(ex);
    });
  }
}

尝试这个:

添加async return (dispatch, getState) => {这样您就可以在其中等待

import {getSavedValue} from '../lib/ValueHandler';
import Api from '../lib/Api';

export function fetchData() {
    return async (dispatch, getState) => {
        let params = [
            'client_id=1234567890',
        ];
        const val = await getSavedValue();
        if (val != null) {
            params = [...params, 'savedValue='+val];
    // why not 
    //      params.push(`savedValue=${val}`);
    //
        }
        params = params.join('&');
        return Api.get(`/posts/5132?${params}`))
        .then(resp => {
                dispatch(setFetchedPosts({ post: resp.posts }));
        })
        .catch( (ex) => {
            console.log(ex);
        });
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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