从服务获取数据时,我想使用Fetch抓取数据后,再赋值给receiveData请问怎么实现?

我想使用Fetch抓取数据后,返回抓取的数据,但是现在我调用postFromServer函数,无法等到Fetch,就直接返回一个undefined,请问如何解决这个问题?

module.exports = {
    postFromServer ( requestParam ) {
            const _self = this;
            let url = requestParam['url'] || '';
            let headers = requestParam['headers'] || '';
            let postBody = requestParam['postBody'] || '';
            let response = _self.sendToServer( url, 'POST', headers, postBody );

            response.then(function(value) {
                console.log('第三方 value === ', value)
                if( value['code'] === 200 ) {
                    return value
                }
            }, function(error) {
                console.log('第三方 error === ', error)
            });
        }
}
import Helper from 'common-helper-es6';
let receiveData = await Helper.postFromServer( requestParam );
阅读 2.5k
2 个回答

加一个return就可以了

module.exports = {
    postFromServer ( requestParam ) {
            const _self = this;
            let url = requestParam['url'] || '';
            let headers = requestParam['headers'] || '';
            let postBody = requestParam['postBody'] || '';
            let response = _self.sendToServer( url, 'POST', headers, postBody );

            return response.then(function(value) {
                console.log('第三方 value === ', value)
                if( value['code'] === 200 ) {
                    return value
                }
            }, function(error) {
                console.log('第三方 error === ', error)
            });
        }
}
return response.then...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏