我使用async / await函数,但是未按预期输出日志。代码的问题在哪里?
export const someFunction = async () => {
let urls: string[] = [];
try {
await request.get(
"https://example.com",
async (error, response, data) => {
const $ = await cheerio.load(data);
$(".some-class").each((index, item) => {
const url = $(item).attr("href");
urls.push(url);
});
}
);
} catch (e) {
console.log(e);
}
console.log(urls);
};
预期结果如下
start
url1
url2
url3
[url1, url2, url3]
当前结果如下
start
[]
url1
url2
url3
我的代码在哪里?