我得到了一个承诺,以回报 React Native 中的 fetch 调用

Tau*_*san 1 reactjs react-native react-native-ios

我想得到一个 json 文件作为回报。

这是我正在使用的提取函数

handleApi(){
    return 
        fetch('https://facebook.github.io/react-native/movies.json')
            .then((response) => response.json())
                .then((responseJson) => {
            return responseJson.movies;
        })
}
Run Code Online (Sandbox Code Playgroud)

正在按钮单击事件上调用此函数。

handleSubmit() {
    console.log(
        this.handleApi();
    )
Run Code Online (Sandbox Code Playgroud)

但我得到这个 Promise对象作为回报不是预期的数据

Promise {_40: 0, _65: 0, _55: null, _72: null}_40: 0_55: null_6​​5: 0_72: null__proto__: Object

dig*_*git 5

更简化的

handleApi(){
    return 
        fetch('https://facebook.github.io/react-native/movies.json')
            .then(response => 
                response.json().then(jsonObj => return jsonObj.movies)
            )
}
Run Code Online (Sandbox Code Playgroud)

然后在 handleSubmit

handleSubmit() {
    this.handleApi().then(movies => {
        console.log('Print list of movies:', movies);
    });
)
Run Code Online (Sandbox Code Playgroud)