Reputation: 2543
const asyncMethod = async () => {
const data = await fetchData()
return data.map(parseResponse)
})
const a = _.debounce(asyncMethod, 0, { leading: true })
Could you call a.cancel()
and also have the async method stop?
Upvotes: 1
Views: 3856
Reputation: 175
I had the same problem. My decision is to use AbortController. And invoke both .cancel()
and .abort()
methods.
Upvotes: 0
Reputation: 18515
Straight from the documentation:
Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the debounced function. Subsequent calls to the debounced function return the result of the last func invocation.
const fn = () => console.log('foo')
const dFn = _.debounce(fn, 500)
dFn()
dFn.cancel() // Remove this to see it working and leave it to cancel
Lets try with setTimeout
:
const fn = () => setTimeout(function(){console.log('foo')}, 200)
const dFn = _.debounce(fn, 500)
dFn()
dFn.cancel() // Remove this to see it working and leave it to cancel
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Now if the async function got to the point of being executed there is nothing you can do really.
Upvotes: 2