fetch取消
fetch取消
function ajax (url = '', options = {}) {
const { timeout, ...opts } = options
const ctrl = new AbortController()
const signal = ctrl.signal
const TIMEOUT = new Response('timeout', { status: 504, statusText: 'timeout' })
const send = fetch(url, { ...rest, signal })
let promises = [send]
if (timeout) {
promises.push(() => new Promise(resolve => {
setTimeout(() => {
resolve(TIMEOUT)
ctrl.abort()
}, timeout)
}))
}
this.abort = function () {
promises.push(() => new Promise(resolve => {
setTimeout(() => {
resolve(TIMEOUT)
ctrl.abort()
})
}))
}
return new Promise((resolve) => {
setTimeout(() => {
resolve(Promise.race(promises))
})
})
}请求并发
LazyMan
观察者模式
Last updated