我正在尝试实现 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()
}