Is it somehow possible using lodash to debounce an async function so it not only be run after a specified delay but also after the latest fired async function finished?
Here is an example:
import _ from "lodash"
const debouncedFunc = _.debounce(async (input: string) => {
return new Promise<void>(resolve => {
console.log(`start ${input}`)
setTimeout(() => {
console.log(`finish ${input}`)
resolve()
}, 5000)
})
}, 1000)
setTimeout(args => {debouncedFunc("0")}, 0)
setTimeout(args => {debouncedFunc("2000")}, 2000)
setTimeout(args => {debouncedFunc("4000")}, 4000)
And it produces the following output:
start 0
start 2000
start 4000
finish 0
finish 2000
finish 4000
And I want it to produce:
start 0
finish 0
start 4000
finish 4000
Does lodash(or some other lib) has means to get this behaviour?