世博会,React Native AsyncStorage无法解决Promise

时间:2018-08-20 15:40:02

标签: react-native promise expo

React Native的AsyncStorage从未兑现其承诺。这是代码:

import {Component} from 'react';
import React from 'react';
import {AsyncStorage} from 'react-native';


export default class Session extends Component {

    componentDidMount() {
        console.log('foo');
        debugger; // This hits
        AsyncStorage.getItem('token').then(() => {
            console.log('baz');
            debugger; // This never hits
        }).catch(() => {
            console.log('catch');
            debugger; // This never hits
        });
        debugger; // This hits
        console.log('bar');
    }


    render() {
        return (
            <React.Fragment>
                {this.props.children}
            </React.Fragment>
        );
    }
}

componentDidMount方法记录“ foo”和“ bar”,但不记录“ baz”或“ catch”

我正在GenyMotion Android模拟器上运行它。

无论我是否处于远程调试模式,其行为都是相同的。

这是我的package.json部门:

"dependencies": {
  "@expo/samples": "2.1.1",
  "eslint-plugin-react-native": "^3.2.1",
  "expo": "29.0.0",
  "prop-types": "^15.6.2",
  "ramda": "^0.25.0",
  "react": "16.3.1",
  "react-connect-context": "^1.1.0",
  "react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",
"react-native-elements": "^1.0.0-beta5",
"react-navigation": "^2.9.3",
"styled-components": "^3.4.2"

}

1 个答案:

答案 0 :(得分:0)

这是React native存在的问题。几个月前,我们遇到了同样的问题。由于我们使用的是Expo,因此我们决定使用Expo SecureStore而不是AsyncStorage,这非常适合我们的要求。

有关该问题的更多信息,请参见here。该问题的标题说,它仅在远程调试时发生,但如果您查看注释,则无论是否进行远程调试都会发生。