Async/Await 请求承诺 returns 未定义
Async/Await with Request-Promise returns Undefined
我有两个文件; server.js 和 scrape.js,下面是它们当前的代码片段。
server.js:
const scrape = require("./scrape");
async function start() {
const response = await scrape.start();
console.log(response);
}
start();
和scrape.js:
const cheerio = require("cheerio");
const request = require("request-promise");
go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};
module.exports = {
start: go
};
所以当我旋转 server.js 时,我 return 未定义到 console.log(响应),而我实际上想要 return 我一直在的数组推到,你能看出我哪里错了吗?
您需要 return
来自您的 async
函数的内容(a 中的 return 不会来自 main 函数的 return)。要么是承诺,要么是你 await
编辑的东西。
此外,请确保声明您的 go
变量以避免将其泄漏到全局 space 中。
const go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
return request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};
由于您使用的是 async
函数,您可能还想利用 await
语法。
const go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
try {
const $ = await request(options);
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
}
catch (err) {
console.log(err);
}
};
我相信您的 go
函数没有返回任何值。
您正在调用 request(options).then(...)
,但 go
永远不会返回该承诺的结果。我建议您添加 return
语句:
go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
// The only difference is that it says "return" here:
return request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};
我有两个文件; server.js 和 scrape.js,下面是它们当前的代码片段。
server.js:
const scrape = require("./scrape");
async function start() {
const response = await scrape.start();
console.log(response);
}
start();
和scrape.js:
const cheerio = require("cheerio");
const request = require("request-promise");
go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};
module.exports = {
start: go
};
所以当我旋转 server.js 时,我 return 未定义到 console.log(响应),而我实际上想要 return 我一直在的数组推到,你能看出我哪里错了吗?
您需要 return
来自您的 async
函数的内容(a 中的 return 不会来自 main 函数的 return)。要么是承诺,要么是你 await
编辑的东西。
此外,请确保声明您的 go
变量以避免将其泄漏到全局 space 中。
const go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
return request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};
由于您使用的是 async
函数,您可能还想利用 await
语法。
const go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
try {
const $ = await request(options);
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
}
catch (err) {
console.log(err);
}
};
我相信您的 go
函数没有返回任何值。
您正在调用 request(options).then(...)
,但 go
永远不会返回该承诺的结果。我建议您添加 return
语句:
go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
// The only difference is that it says "return" here:
return request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};