[英]How to call async function inside request-promise object?
我有一个 function 给出如下:
var request = require('request-promise');
var gzipEncoding=require('./gzipClass');
(async()=>{
request({headers:{"Accept-Encoding": "gzip"},uri:"https://www.giftofspeed.com/gzip-test/", method: 'HEAD'}, function (err, res, body){
console.log(res.headers['content-encoding']);
const result= await gzipEncoding.checkForGzipEncoding(res.headers)
console.log(result)
});
})()
但我得到这个错误
const result= await gzipEncoding.checkForGzipEncoding(res.headers)
^^^^^
SyntaxError: await is only valid in async function
T 认为是因为我在请求中调用了异步 function。 如何在请求中调用异步 function? 提前致谢。
您需要在调用request
时将async
添加到回调中。
request({
headers: { "Accept-Encoding": "gzip" }, uri: "https://www.giftofspeed.com/gzip-test/", method: "HEAD" },
async function (err, res, body) {
console.log(res.headers["content-encoding"]);
const result = await gzipEncoding.checkForGzipEncoding(res.headers)
console.log(result);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.