Do not understand what you are trying to accomplish, as this is highly dependent on your needs. However, there is something in common. Without a solid understanding of what happens in the code below, you really may not want to use it, though.
View the console as you run the code above. I put a few posts to talk a bit about what is happening. The first function that returns the promise is passed as the argument to new debounce() . This creates a debounced version of the function.
When you run the debounced function, as the above code ( a(1), a(2), and a(3) ) shows, you will notice that during processing it returns the same promise, rather than starting a new one. Once the promise is completed, it will delete the old promises. In the above code, I expect a timeout manually using setTimeout before running (3).
You can also clear the promise in other ways, for example, add a reset or clear function to debounce.prototype to clear the promise at another time. You can also set a timeout. Tests in the console log should show that p1 and p2 receive the same promise (link comparison === "true) and that p3 is different.
source share