Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用async/await从回调返回对象_Javascript_Node.js_Asynchronous_Callback_Async Await - Fatal编程技术网

Javascript 使用async/await从回调返回对象

Javascript 使用async/await从回调返回对象,javascript,node.js,asynchronous,callback,async-await,Javascript,Node.js,Asynchronous,Callback,Async Await,我无法用的答案解决此问题,因为代码中存在差异 我想从回调中返回一个对象。当我运行下面的代码时,body对象的日志看起来与预期的一样。它似乎是正确的JSON对象,包含我希望从服务器得到的响应:名称、电子邮件、网站等 但是结果对象似乎包含关于请求本身的信息,而不是响应对象 如何返回body对象,以便从结果变量访问它 const request=要求“请求”;//npm i请求-s module.exports=异步配置=>{ ... const result=wait request.get url

我无法用的答案解决此问题,因为代码中存在差异

我想从回调中返回一个对象。当我运行下面的代码时,body对象的日志看起来与预期的一样。它似乎是正确的JSON对象,包含我希望从服务器得到的响应:名称、电子邮件、网站等

但是结果对象似乎包含关于请求本身的信息,而不是响应对象

如何返回body对象,以便从结果变量访问它

const request=要求“请求”;//npm i请求-s module.exports=异步配置=>{ ... const result=wait request.get url、选项、, 错误,响应,正文,=>{ console.log“body”,body;//我希望另一个日志与此日志类似。
body {
  "name": "foo",
  "email": "foo@example.com",
  "website": "www.example.com",
  ...
}
返回体; } ; console.log'result',result;//我希望此日志与上面的日志类似。
result Request {
  _events: [Object: null prototype] {
    error: [Function: bound ],
    complete: [Function: bound ],
    pipe: [Function]
  },
  _eventsCount: 3,
  _maxListeners: undefined,
  uri: Url {
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'api.example.com',
    port: 443,
    hostname: 'api.example.com',
    hash: null,
  },
  callback: [Function],
  method: 'GET',
  readable: true,
  writable: true,
  explicitMethod: true,
  _qs: Querystring {
    request: [Circular],
    lib: { formats: [Object], parse: [Function], stringify: [Function] },
    useQuerystring: undefined,
    parseOptions: {},
    stringifyOptions: {}
  },
  _auth: Auth {
    request: [Circular],
//换句话说,我希望下面一行是名称、电子邮件、网站JSON对象 //藏在体内 返回结果; } 这就是我想要的结果

console.log“body”,body;
body {
  "name": "foo",
  "email": "foo@example.com",
  "website": "www.example.com",
  ...
}
这就是我从结果中得到的

console.log“result”,result;
result Request {
  _events: [Object: null prototype] {
    error: [Function: bound ],
    complete: [Function: bound ],
    pipe: [Function]
  },
  _eventsCount: 3,
  _maxListeners: undefined,
  uri: Url {
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'api.example.com',
    port: 443,
    hostname: 'api.example.com',
    hash: null,
  },
  callback: [Function],
  method: 'GET',
  readable: true,
  writable: true,
  explicitMethod: true,
  _qs: Querystring {
    request: [Circular],
    lib: { formats: [Object], parse: [Function], stringify: [Function] },
    useQuerystring: undefined,
    parseOptions: {},
    stringifyOptions: {}
  },
  _auth: Auth {
    request: [Circular],
使用承诺:

编辑:

或者您可以安装

request.get不会返回承诺,因此wait on不会执行任何有用的操作。如果我每次回答这个问题都有一个五分镍币,我就会有很多五分镍币。等待没有魔力。它所做的只是等待一个承诺。如果你不给它一个承诺,它什么也做不了

您可能不知道,但请求模块及其衍生产品没有新功能,只有bug修复。您可以使用request-promise模块,然后摆脱回调,也可以切换到正在积极开发的较新模块,如got模块。这将是我的建议:

const got = require('got');

module.exports = async config => {
  ...

  const result = await got(url, options);

  console.log( 'result', result ); // I want this log to look like the above log.
  // In other words, I want the below line to be the name, email, website JSON object
  // contained in the body
  return result;
}
我希望您认识到,所有异步函数都会返回一个承诺,因此此函数的调用方必须使用wait或.然后在返回的承诺上使用,以便从承诺中获得结果

如果您想继续使用请求库,可以使用请求承诺来获取库的已承诺版本:

const rp = require('request-promise');

module.exports = async config => {
  ...

  const result = await rp(url, options);

  console.log( 'result', result ); // I want this log to look like the above log.
  // In other words, I want the below line to be the name, email, website JSON object
  // contained in the body
  return result;
}

request.get不会返回承诺,因此wait on不会执行任何有用的操作。如果我每次回答这个问题都有一个五分镍币,我就会有很多五分镍币。等待没有魔力。它所做的只是等待一个承诺。如果你不给它一个承诺,它不会做任何事情。仅供参考,请求模块已进入,并且将不再获得新功能。不建议使用它编写新代码。@jfriend00维护模式并不意味着不受支持。这只是意味着开发人员不觉得需要添加任何其他内容。如果它拥有您所需要的一切,那么在新代码中使用它是非常好的。@Tarazed-嗯,它是死胡同代码。如果nodejs实现了一种新类型的流,它更友好地用于承诺,那么您认为请求模块会支持这种新类型的流吗?我不。它正在修复bug,就这样。如果您正在编写新代码,最好选择一个正在积极开发的替代方案。现在,如果您现在使用请求库,世界不会在明天结束,但我认为这不是最明智的新代码选择。同样值得理解的是,为什么它被置于维护模式。非常感谢+1.您提供的关于async和request and Get模块的附加上下文一针见血。