Q: How to add a timestamp?
A: Date.now()
Q: How to log into a file?
A: The most simple way is using packages, like winstonjs.
Q: How to verify if the intermittent delays?
A: Well, you can try to simulate timeout for fetch API:
const timeout = 200;
try{
let timer = setTimeout(() => {
throw new Error('Timeout');
}, timeout);
const html = await fetch(...).then(r => r.text());
clearTimeout(timer);
return html;
} catch (e) {
// if timeout, code goes here
return false;
}
EDIT:
Here is an example for you:
// edit your getWebsite function
async function getWebsite() {
const timeout = 200;
try{
let timer = setTimeout(() => {
throw new Error('Timeout');
}, timeout);
const html = await fetch(...).then(r => r.text());
clearTimeout(timer);
const doc = new JSDOM(html).window.document;
const links = Array.from(doc.querySelectorAll('.card-title a')).map(({ textContent, href }) => ({ textContent, href }));
return links;
} catch (e) {
// if timeout, code goes here
return false;
}
}
// and your compare logic should be
async function compare() {
const links = await getWebsite();
if (links === false) {
// if code goes here, there is something went wrong in getWebsite function, like timeout or code issue
// you can log server being throttled/firewalled here
compare(); // compare infinitely
return;
}
const linkIDs = links.map(l => {
parts = l.href.split('/');
parts.pop();
return parts.pop();
});
...
// compare infinitely
compare();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…