如何使用等待在 reactjs 中每 2 分钟调用一次操作
how to call action after every 2 minute in reactjs with await
我正在使用 async await 调用 API 进行响应,我的问题是如何每 2 分钟调用一次 get 服务,因为在我的情况下,promise 没有解决它的占用时间并显示挂起状态,因此,如果未解决承诺,我想每 2 分钟拨打一次服务电话。有人可以指导我如何使用 await 来做到这一点。
这是我所做的代码。
export function fetchUser(id) {
return async dispatch => {
let userUrl = FETCH_USER_URL + id;
let response1 = await get(userUrl); // I want to call this again if response not getting within a 2 minutes
if (response1.status === 200) {
let processUrl = FETCH_PROCESS_USER_URL;
let response2 = await get(processUrl);
if (response2.status === 200) {
console.log("fetch process user success");
} else {
console.log("fetch process user failed");
}
console.log("success");
} else {
console.log("failed");
}
};
}
任何帮助将不胜感激。提前致谢
你可以试试这个
const timeout = setTimeout(fetchUser(id), 120000);
let response = await get(url);
clearTimeout(timeout);
因评论而编辑:
export function fetchUser(id) {
return async dispatch => {
let url = FETCH_USER_URL + id;
const timeout = setTimeout(fetchUser(id), 120000); // start 2 min timer that will call the function again in 2 min
let response = await get(url); // if response not getting within a 2 minutes call again
clearTimeout(timeout); //remove it if you get a response before 2min
if (response.status === 200) {
let url2 = FETCH_PROCESS_USER_URL;
let response2 = await get(url);
if (response2.status === 200) {
console.log("fetch process user success");
} else {
console.log("fetch process user failed");
}
console.log("success");
} else {
console.log("failed");
}
};
}
,,,
请试试这个方法。
const NUM_RETRIES = 3;
test();
async function getData() {
let arr = new Array(NUM_RETRIES).map(() => null);
arr.forEach(() => {
try {
await get('url');
} catch (err) {}
});
}
我正在使用 async await 调用 API 进行响应,我的问题是如何每 2 分钟调用一次 get 服务,因为在我的情况下,promise 没有解决它的占用时间并显示挂起状态,因此,如果未解决承诺,我想每 2 分钟拨打一次服务电话。有人可以指导我如何使用 await 来做到这一点。
这是我所做的代码。
export function fetchUser(id) {
return async dispatch => {
let userUrl = FETCH_USER_URL + id;
let response1 = await get(userUrl); // I want to call this again if response not getting within a 2 minutes
if (response1.status === 200) {
let processUrl = FETCH_PROCESS_USER_URL;
let response2 = await get(processUrl);
if (response2.status === 200) {
console.log("fetch process user success");
} else {
console.log("fetch process user failed");
}
console.log("success");
} else {
console.log("failed");
}
};
}
任何帮助将不胜感激。提前致谢
你可以试试这个
const timeout = setTimeout(fetchUser(id), 120000);
let response = await get(url);
clearTimeout(timeout);
因评论而编辑:
export function fetchUser(id) {
return async dispatch => {
let url = FETCH_USER_URL + id;
const timeout = setTimeout(fetchUser(id), 120000); // start 2 min timer that will call the function again in 2 min
let response = await get(url); // if response not getting within a 2 minutes call again
clearTimeout(timeout); //remove it if you get a response before 2min
if (response.status === 200) {
let url2 = FETCH_PROCESS_USER_URL;
let response2 = await get(url);
if (response2.status === 200) {
console.log("fetch process user success");
} else {
console.log("fetch process user failed");
}
console.log("success");
} else {
console.log("failed");
}
};
}
,,,
请试试这个方法。
const NUM_RETRIES = 3;
test();
async function getData() {
let arr = new Array(NUM_RETRIES).map(() => null);
arr.forEach(() => {
try {
await get('url');
} catch (err) {}
});
}