繁体   English   中英

尝试检索异步/等待的结果

[英]Trying to retrieve result of async/await

我正在尝试使用async / await从诺言中检索结果,但是收到以下错误:

544:1未捕获的错误:模块构建失败:语法错误:等待是保留字(30:23)

这是我的代码:

 import React from 'react'; import Header from './Header'; import Feed from './Feed'; import _ from 'lodash'; const Cosmic = require('cosmicjs')(); export default class WhereSheGoes extends React.Component { constructor (props) { super(props); this.state = { destinations: '', posts: '', globals: '', } } render() { const bucket = Cosmic.bucket({ slug: 'where-she-goes', read_key: '', write_key: '' }); const retrieveBucket = async () => { try { let result = bucket.getBucket() return result } catch (err) { console.log(err) } } const result = await retrieveBucket() console.log(result) this.setState (() => { return { destinations: _.filter(result.bucket.objects, {type_slug: 'destinations'}), posts: _.filter(result.bucket.objects, {type_slug: 'posts'}), globals: _.filter(result.bucket.objects, {type_slug: 'globals'}) } }); return ( <div> <Header destinations={this.state.destinations} posts={this.state.posts} globals={this.state.globals} /> <Feed posts={this.state.posts} destinations={this.state.destinations} /> </div> ); } } 

我可以让以上代码运行,但前提是要返回结果并在实际的异步函数中设置状态。 如何在该函数之外返回Promise的结果?

谢谢!

await只能在用async声明的函数内部使用。 因此,您应该在此函数内使用await来获取数据并设置状态。 同样,您的代码也不是最佳的,因为它将尝试在每次重新呈现组件时接收数据。 更好地构造您的类,并使用诸如componentDidMount类的生命周期方法来获取数据并将其存储到状态。 然后在渲染中只使用状态显示它

export default class WhereSheGoes extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            destinations: '',
            posts: '',
            globals: '',
        }
    }

    componentDidMount() {
        retrieveBucket();
    }

    retrieveBucket = async () => {
        const bucket = Cosmic.bucket({
            slug: 'where-she-goes',
            read_key: '',
            write_key: ''
        });
       try {
          let result = await bucket.getBucket()
          console.log(result)
          this.setState ({
                destinations: _.filter(result.bucket.objects, {type_slug: 'destinations'}),
                posts: _.filter(result.bucket.objects, {type_slug: 'posts'}),
                globals: _.filter(result.bucket.objects, {type_slug: 'globals'})
            });
       } catch (err) {
            console.log(err)
       }
    }

    render() {
        return (
            <div>
                <Header 
                    destinations={this.state.destinations}
                    posts={this.state.posts}
                    globals={this.state.globals}
                />
                <Feed
                    posts={this.state.posts}
                    destinations={this.state.destinations}
                />
            </div>
        );
    }
}```

您只能在异步函数中使用await

您也可以使用.then

从'react'导入React; 从'./Header'导入Header; 从“ ./Feed”导入Feed; 从'lodash'导入_; const Cosmic = require('cosmicjs')();

export default class WhereSheGoes extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            destinations: '',
            posts: '',
            globals: '',
        }
    }

    retriveBucket = () => {
      const bucket = Cosmic.bucket({
          slug: 'where-she-goes',
          read_key: '',
          write_key: ''
      });

      try {
        bucket.getBucket().then(result => {
          this.setState (() => {
              return {
                  destinations: _.filter(result.bucket.objects, {type_slug: 'destinations'}),
                  posts: _.filter(result.bucket.objects, {type_slug: 'posts'}),
                  globals: _.filter(result.bucket.objects, {type_slug: 'globals'})
              }
          });
        });
      } catch (err) {
          console.log(err)
      }
    }

    render() {
        this.retriveBucket();

        return (
            <div>
                <Header 
                    destinations={this.state.destinations}
                    posts={this.state.posts}
                    globals={this.state.globals}
                />
                <Feed
                    posts={this.state.posts}
                    destinations={this.state.destinations}
                />
            </div>
        );
    }
}

暂无
暂无

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

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