// function request(params) {
// return new Promise(resolve => {
// setTimeout(() => {
// resolve(params)
// }, 3000)
// })
// }
// function timeoutWrap(p, timeout) {
// const delay = new Promise((_, reject) => {
// setTimeout(() => {
// reject('超时了')
// }, timeout)
// })
// return (...args) => {
// const p1 = p(...args);
// const res = Promise.race([p1, delay]);
// return res
// }
// }
// const r1 = timeoutWrap(request, 1000)({ id: 1 })
// r1.then(response => {
// console.log(response)
// }).catch(e => {
// console.log('e', e)
// })
const funWait = (call, timeout = 5000) => {
let wait = new Promise((resolve, reject) => {
setTimeout(() => {
reject('请求超时')
}, timeout)
})
return Promise.race([call(), wait])
}
const t = () => new Promise(resolve => setTimeout(resolve, 4000))
const t2 = () => new Promise(resolve => setTimeout(resolve, 6000))
funWait(t).then(res => {
console.log("t1 resolve")
}).catch(err => {
console.log("t1 timeout")
})
funWait(t2).then(res => {
console.log("t2 resolve")
}).catch(err => {
console.log("t2 timeout", err)
})
console