Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
Node.js https.get的异步/等待_Node.js_Async Await - Fatal编程技术网

Node.js https.get的异步/等待

Node.js https.get的异步/等待,node.js,async-await,Node.js,Async Await,我试图用async/await简化代码 但是在使用异步/等待结构生成https.get时遇到问题 我知道如何使用第三方模块实现这一点,但更喜欢native node.jshttps模块 以下代码不适用于我: 异步函数get_page(){ 常量https=require('https') 常量url=https://example.com' const util=require('util') const https_get=util.promisify(https.get) const dat

我试图用async/await简化代码

但是在使用异步/等待结构生成
https.get
时遇到问题

我知道如何使用第三方模块实现这一点,但更喜欢native node.js
https
模块

以下代码不适用于我:

异步函数get_page(){ 常量https=require('https') 常量url=https://example.com' const util=require('util') const https_get=util.promisify(https.get) const data=wait https\u get(url) 用数据做令人敬畏的事情(数据) } 此代码运行良好:

函数get_page(){
常量https=require('https')
常量url=https://example.com'
让数据=“”
https.get(url,res=>{
res.on('data',chunk=>{data+=chunk})
res.on('结束',()=>{
用数据做令人敬畏的事情(数据)
})
}) 
}

https.get
不会返回可能的内容,因为回调的签名与
(err,value)
不匹配,因此您无法返回它

但是,您可以将
https.get
调用封装在一个承诺中,就像这样,然后在调用
get\u页面时等待

const https=require('https')
异步函数get_page(){
常量url=https://example.com'
返回新承诺((解决)=>{
https.get(url,res=>{
res.on('data',chunk=>{data+=chunk})
res.on('结束',()=>{
解决(用数据(数据)做事情);
})
}) 
})
}
//用法
(异步()=>等待获取页面())()
编辑
我已经更新了我的答案,加入了
https的注释。get
无法被promisified,并将
require('https')
移动到函数调用之外。

而不是promisify,滚动您自己的函数,或者使用第三方库。Promisify无法包装https.get返回的内容

// generic promise method for https
const requestPromise = ((urlOptions, data) => {
  return new Promise((resolve, reject) => {
    const req = https.request(urlOptions,
      (res) => {
        let body = '';
        res.on('data', (chunk) => (body += chunk.toString()));
        res.on('error', reject);
        res.on('end', () => {
          if (res.statusCode >= 200 && res.statusCode <= 299) {
            resolve({statusCode: res.statusCode, headers: res.headers, body: body});
          } else {
            reject('Request failed. status: ' + res.statusCode + ', body: ' + body);
          }
        });
      });
    req.on('error', reject);
    req.write(data, 'binary');
    req.end();
  });
});

或者简单地使用一个库,比如
axios
来返回一个本机承诺,并处理额外的样板案例。

我认为在数据未解析时,您可以调用
使用数据(数据)
做一些了不起的事情。只是为了调试,您可以在函数调用和promise之间设置1秒的超时时间。问题是
get
不会返回
promisify
可以包装的内容。OP知道没有返回承诺。
require()
不应在
async
函数中使用,因为它是同步的。在一个会被重复调用的函数中使用
require()
也是一种不好的做法,通常
util.promisify()
也是如此。它们都应该移到函数之外。是的,这里的两个注释都是正确的。我将更新我的答案以反映。这是一个优雅的最低限度的答案,但我不明白为什么get_page函数需要有异步前缀。当我在没有前缀的情况下实现这个模式时,它的工作原理是一样的。我的理解是,async允许使用wait前缀,但您没有前缀,并且承诺使用非承诺返回值,但您不返回任何值。这是正确的@JonathanPool-默认情况下,在这种情况下,您不需要
async
前缀,因为我们不需要
wait
等待。为了保持一致性,并让下一位工程师知道谁将使用
get\u page
函数,他们将立即知道此函数返回一个承诺,并且需要使用
.then()
wait
async function get_page() {
    const url = 'https://example.com'
    const data = await requestPromise({url, method:'GET'})
    do_awesome_things_with_data(data)
}