首页 > 其他分享 >实现带有超时功能的promise

实现带有超时功能的promise

时间:2022-09-26 14:33:31浏览次数:51  
标签:function resolve return res promise reject 带有 超时 data

let rest = function (_data = 4000) {
return Promise.race([
upload(),
Timeout(_data)
])
}
function upload() {
console.log('请求进行中...');
return new Promise((resolve, reject) => {
//模拟的接口
setTimeout(() => {
resolve({ data:{code:200,data:[]} })
}, 2000);
// 向外暴露取消函数
cancelFn = function (msg) {
reject('请求超时,请重试');
}

})
};
function Timeout(times) {
return new Promise((resolve, reject) => {
setTimeout(() => {
cancelFn();
}, times)
})
}
let res= rest()
res.then(res=>{
console.log(res);
})

标签:function,resolve,return,res,promise,reject,带有,超时,data
From: https://www.cnblogs.com/judyhuayu/p/16730872.html

相关文章