关于本课setTimeOut 延时的问题
我看到视频中setTimeOut 延时为0,也能够起到在abort之后再缓存的作用。但是我这里必须延时大于0,否则就不起作用,这是为什么呢?

export const request = (options = {}) => {
const result = new Promise((resolve, reject) => {
// 兼容 request 传入的 options 中含有成功或失败的回调
const {success, fail} = options
const key = options.url + '&' + (options.method || 'GET')
const handler = wx.request(Object.assign(
{}, options,
{
success: res => {
delete pending[key]
if (isHttpSuccess(res.statusCode)) {
if (success) {
success(res.data)
return
}
resolve(res.data)
} else {
errorHandler(res)
reject(res)
}
},
fail: err => {
delete pending[key]
errorHandler(err)
if (fail) {
fail(err.data)
return
}
reject(err)
}
})
)
if (pending[key]) {
pending[key].abort()
}
// setTimeout 能够保证在每次 abort() 之后存值
setTimeout(() => {
pending[key] = handler
}, 0)
})
return result
}13
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星