I have the following code
const link =
"https://www.daft.ie/property-for-rent/ireland?location=dublin&location=dublin-city&sort=publishDateDesc";
async function getLinks(url) {
return fetch(url)
.then((res) => res.text())
.then((html) => {
const $ = cheerio.load(html);
const sel = '[data-testid="results"] a[href]';
var links = [...$(sel)].map((e) => e.attribs.href);
return Promise.all(links);
});
}
getLinks(link).then(function(links) {
console.log(links);
});
This returns me an object array like this
[
'/for-rent/apartment-105-cabra-road-phibsborough-dublin-7/4071977',
'/for-rent/apartment-weavers-hall-leopardstown-dublin-18/4073220',
]
I would like this to be returned as array of strings so I can perform comparison operations more easily and so I can store the array in a variable.
Also I want know how to use await
in this
I want it to work like this but it fails currently
const a = await getLinks(link1);
const b = await getLinks(link2);
where a
and b
contain string arrays.
How can I do this?
CodePudding user response:
You cannot call await
unless it is within an async
context. It's like calling then
on a non-Promise object. You could wrap it within an anonymous async method such as:
(async () => {
const a = await getLinks(link1);
const b = await getLinks(link2);
// do something
})()
CodePudding user response:
You can get the first part of the URL with the built-in URL api:
const link = "https://www.daft.ie/property-for-rent/ireland?location=dublin&location=dublin-city&sort=publishDateDesc";
const url = new URL(link);
console.log(url.protocol "//" url.host);
Then you can append the links you get to this:
const url = new URL(link);
const a = await getLinks(url.protocol "//" url.host link1);
const b = await getLinks(url.protocol "//" url.host link2);
If you want to use await
at the top level, and if the environment doesn't support top-level await, then yes you'll have to create an async IIFE:
(async () => {
// your code
})();