Axios断连重试





    
    axios基本使用



    





Axios断连重试_第1张图片 

 

Promise.race判断请求是否超时 

let controller = new AbortController()
let signal = controller.signal

let timeoutPromise = (timeout) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("我是 timeoutPromise,已经完成了")
            controller.abort()
        }, timeout)
    })
}

let requestPromise = (url) => {
    return fetch(url, {
        signal
    })
}

Promise.race([
   timeoutPromise(1000),
   requestPromise("https://jsonplaceholder.typicode.com/todos/1")
])
.then(resp => {
   console.log(resp)
})
.catch(error => {
   console.log(error)
})

你可能感兴趣的:(javascript)