You are basically not waiting for the function to finish before you run response.send(arrNews).
You should use async/await to wait untill the function is finished.
Some thing like that:
app.post("/", async (req, response) => {
let arrNews = [];
console.log("Cookies: ", req.cookies);
const data = req.body;
console.log(data);
let myResponse;
if (data.Football === "on") {
const result = await getData({
site: "https://www.euro-football.ru/",
selector: ".main-news__item",
number: data.numberNews,
})
arrNews.push("Футбол");
arrNews = arrNews.concat(result);
}
if (data.F1 === "on") {
const result = await getData({
site: "https://www.f1news.ru/",
selector: ".b-news-list__title",
number: data.numberNews,
})
arrNews.push("Ф1");
arrNews = arrNews.concat(result);
}
........
response.send(arrNews)
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…