在声明新的承诺之前,可以使用两个承诺来等待检查URL状态的方法:
示例代码:
const promises = [
fetch('https://example.com'),
fetch('https://example.org'),
fetch('https://example.net')
];
Promise.allSettled(promises)
.then(results => {
results.forEach(result => {
if (result.status === 'fulfilled') {
console.log('URL is reachable:', result.value.url);
} else {
console.log('URL is not reachable:', result.reason.url);
}
});
})
.catch(error => {
console.error('Error occurred:', error);
});
推荐的腾讯云相关产品:腾讯云函数(云函数是腾讯云提供的无服务器计算服务,可以实现按需执行代码的功能,无需关心服务器的运维,支持多种编程语言,如JavaScript、Python等。可以将上述代码封装为云函数,以实现自动检查URL状态的功能。)
腾讯云函数产品介绍链接地址:https://cloud.tencent.com/product/scf
示例代码:
async function checkURLStatus(url) {
try {
const response = await fetch(url);
console.log('URL is reachable:', url);
} catch (error) {
console.log('URL is not reachable:', url);
}
}
async function waitForURLStatus() {
const urls = ['https://example.com', 'https://example.org', 'https://example.net'];
for (const url of urls) {
await checkURLStatus(url);
}
}
waitForURLStatus()
.catch(error => {
console.error('Error occurred:', error);
});
推荐的腾讯云相关产品:腾讯云云函数(与Promise.allSettled()方法相同的原理,使用云函数来实现异步操作。)
腾讯云云函数产品介绍链接地址:https://cloud.tencent.com/product/scf
领取专属 10元无门槛券
手把手带您无忧上云