You need to return
something from your async
function (a return inside a then does not return from the main function). Either a promise or something you await
-ed.
Also, make sure to declare your go
variable to avoid leaking it into the global 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);
});
};
Since you are using an async
function, you might want to take advantage of the await
syntax also.
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);
}
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…