IF api taking time then it should notify within 5 sec
const result = await API.methodForAPI()
// if Noresponse from API in 5 sec show alert
if(result)
IF api taking time then it should notify within 5 sec
const result = await API.methodForAPI()
// if Noresponse from API in 5 sec show alert
if(result)
You can try with Promise.race, to find what returns first, the API call or the 5 sec timeout.
var p1 = fetch('https://reqres.in/api/users?page=2');
var p2 = new Promise((resolve,reject)=>{ setTimeout(resolve, 5000, new Error('timeout'))
var p1 = fetch('https://reqres.in/api/users?page=2')
var p2 = new Promise((resolve,reject)=>{ setTimeout(resolve, 5000, new Error('timeout')); });
Promise.race([p1,p2]).then((res)=>{
if(res instanceof Error) {
console.warn(res);
} else {
res.json().then((users)=> {
console.log(users);
});
}
})
; })