如何在请求承诺对象中调用异步函数?
How to call async function inside request-promise object?
我有一个函数如下:
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
我不这么认为,因为我在请求中调用了一个异步函数。如何在请求中调用异步函数?提前致谢。
调用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);
}
});
我有一个函数如下:
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
我不这么认为,因为我在请求中调用了一个异步函数。如何在请求中调用异步函数?提前致谢。
调用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);
}
});