I m trying to perform a synchronous loop until a fetch to a URL is successful. Here s what I ve attempted:
let res = null
fetch("https://jsonplaceholder.typicode.com/users/1").then((response)=>{
return response.json();
}).then((data)=>{
res = data;
return data
})
while(!res){
console.log(`${Date.now()} fetching`);
}
However, it doesn t work because the callback is not executed, leading to res = null. How can I modify this to perform a synchronous loop until a fetch to a URL is successful?