I am currently working on a JavaScript function that will iterate through an array of URLs to find a 'pingable' URL and return its index. Here is the code snippet I have so far:
function ping(url: string): Promise {
return new Promise((resolve) => {
this.http.get(url, { observe: 'response' }).subscribe(
(response) => {
resolve(response.status === 200);
},
(error) => {
resolve(false);
}
);
});
}
I am facing a challenge with handling promises within each iteration of the loop. Any advice or guidance would be greatly appreciated.
My attempt involved using a for loop with a break statement to exit once a pingable URL was found. However, breaks are not permitted in this case.