Git Product home page Git Product logo

asyncdebounce's Introduction

Async debounce

With a debounced function the promise returned by the function is lost so we can’t await it.

debounce is not using the promise returned by seconds, it ignores the return value of the debounced function.

With await

src/await.js

node src/await.js

await wraps whatever you pass to it in a promise so by using it we can’t see that the debounced function actually returns undefined.

You can see the DEBOUNCE timer is completed only 9ms instead of waiting for 3 seconds as the non-debounced function does.

Initialised
DEBOUNCE: 9.749ms
PLAIN: 3.003s

src/await-fixed.js

node src/await-fixed.js

By using asyncDebounce we get a promise that will only be completed after the debounce time has passed and the function's return promise has been fulfilled.

Initialised
PLAIN: 3.002s
DEBOUNCE: 3.109s

With .then()

src/then.js

node src/then.js

If we use promise.then() instead of await we can see the debounced function actually returns undefined

You can see the DEBOUNCE timer is completed only 9ms instead of waiting for 3 seconds as the non-debounced function does.

/debounce/src/then.js:9
debounced(3).then(() => console.timeEnd('DEBOUNCE'));
            ^
TypeError: Cannot read property 'then' of undefined
    at Object.<anonymous> (/debounce/src/then.js:9:13)

src/then-fixed.js

node src/then-fixed.js
Initialised
PLAIN: 3.004s
DEBOUNCE: 3.106s

continuation-passing style

A way of doing this with _.debounce is to avoid the usage of promises and return to continuation-passing style. That is the use of callbacks.

Avoiding promises also mean async/await can't be used.

src/cps.js

node src/cps.js
Initialised
PLAIN: 3.004s
DEBOUNCE: 3.104s

asyncdebounce's People

Contributors

amatiasq avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.