在Axios请求中使用Lodash的Debounce会产生意外的结果

时间:2016-10-01 18:01:30

标签: javascript reactjs ecmascript-6 lodash axios

每次输入文字时,我的应用都会更新。然后在axios请求上使用去抖动,请求排队,直到计时器用完,然后它们全部立即进入。我试图将请求限制为每10秒一个。我哪里错了?

如果有这样的后果,我会在ReactJS中这样做。

const debouncedSync = _.debounce(() => {this.sync () }, 10000);

sync = (requestBody) => {
    axios.post('http://127.0.0.1:8000/Server/', requestBody);
  };

1 个答案:

答案 0 :(得分:0)

请检查faxios debounce

let fetcher = faxios()
.baseURL('http://jsonplaceholder.typicode.com')
.url('posts', 1, 'comments')
.debounce(1 * 1000); // debounce time 1000ms

fetcher
.FETCH // => Promise
.then(res => console.log('res1:',res))
.catch(err => console.log('error1:', err));

fetcher
.FETCH // => Promise
.then(res => console.log('res2:', res))
.catch(err => console.log('error2:', err));

fetcher
.FETCH // => Promise
.then(res => console.log('res3:',res))
.catch(err => console.log('error3:', err));

fetcher
.FETCH // => Promise
.then(res => console.log('res4:', res))
.catch(err => console.log('error4:', err));


/**

error1:debounced

error2:debounced

error3:debounced

res4:{...} 

**/