我正在尝试实现cancelToken API,以便每当我发出请求从服务器检索结果时,只有最后一个请求会被处理,但是我已经实现了axios取消令牌API,它似乎会取消每个请求,这里我做错了什么?这个函数在每个按键上都会被调用。
getProductLists2 = async () => {
let formData = new FormData()
const { start, limit } = this.state
formData.append('start', start)
formData.append('limit', limit)
formData.append('product_title', this.state.searchTitle)
formData.append('product_sku', this.state.searchSKU)
let cancel;
Axios({
method: 'POST',
url: BASE_URL + '/listProduct',
// headers: { 'Content-Type': 'multipart/form-data' },
data: formData,
cancelToken: new Axios.CancelToken(c => cancel = c)
})
.then(products => {
if (products && products.data.productList && products.data.productList.length > 0) {
this.setState({ productList: products.data.productList, totalProducts: products.data.productTotal })
}
})
.catch(err => toast.error('Something went wrong'))
console.log(cancel, 'h')
cancel && cancel()
}