请求五秒未完成则终止
Sunny-117 opened this issue · comments
请求五秒未完成则终止
function _end() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject("error");
}, 5000);
});
}
let promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1);
}, 10000);
});
let promise2 = _end();
Promise.race([promise2, promise1])
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
function sendRequestMock() {
return new Promise(resovle => {
setTimeout(() => {
resovle("success")
}, 4 * 1000);
})
}
function timePromise() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('the request is interrupted, beacause of overtime')
}, 3000);
})
}
async function reqeustWithOvertime() {
try {
const result = await Promise.all([sendRequestMock(), timePromise()])
} catch (error) {
console.error(error)
}
}
reqeustWithOvertime()