I have the following code:
import axios from "axios";
export async function get(url: string, n: number = 10) {
let error;
for (let i = 0; i < n; i++) {
try {
console.log("This get executed");
await axios({ method: "get", url: url, responseType: "document" });
} catch (err) {
console.log("This code does not");
error = err;
}
}
throw error;
}
get("https://manganeo.com/")
.then(response => console.log(response))
.catch(err => console.log(err));
What I am trying to do is repeat the petition until its success. If you call get
with an existing url it works well. Nonetheless, if the url returns a 404 error, this function gets hanged.
Any idea what is happening?
It seems Axios does not come with a default timeout set, so I'm gonna guess your request never gets timed out and it hangs indefinitely.
Try again with a timeout and see if that fixes it:
await axios({ method: "get", url: url, timeout: 10000, responseType: "document" });