Coder Social home page Coder Social logo

Comments (6)

benjamingr avatar benjamingr commented on August 16, 2024 2

These are async functions, when you await anything in them even if that promise is already fulfilled or it's not even a promise it will defer a microtick before continuing to run.

The reason your bluebird example sometimes "works" is that bluebird internally knows some functions are supposed to be async like setTimeout so it skips that microtick which is done here: https://github.com/petkaantonov/bluebird/blob/master/src/timers.js#L32 and subsequently the microtick is skipped here: https://github.com/petkaantonov/bluebird/blob/master/src/promise.js#L714-L717

Basically bluebird is making the assumption setTimeout is async which is fair and the mocks break that assumption which messes with timing. A little like if you do Promise.setScheduler(fn => fn()).

This is not a bug in fake-timers and there is nothing we can do to make the native promises behave like bluebird anyway - you need to await those function calls :)

from fake-timers.

abel-matsec avatar abel-matsec commented on August 16, 2024 1

@benjamingr I'm not sure it's just a bluebird caveat. I show in my above example me defining my own util (i.e. not using bluebird), and observing similar odd behavior. I can look into maybe starting a docs PR, though I'm not sure I'll be able to explain the subtleties of tick execution as well as you guys can, but I can take a crack at it regardless.

        function delay<T>(ms: number, value?: T): Promise<T | undefined> {
            return new Promise(($resolve, $reject) => {
                setTimeout(() => {
                    $resolve(value);
                }, ms);
            });
        }

from fake-timers.

fatso83 avatar fatso83 commented on August 16, 2024

All good then, I suppose 😸?

from fake-timers.

abel-matsec avatar abel-matsec commented on August 16, 2024

Oh weird. Yeah, not all good :P didn't realize that mentioning this PR in our internal repo would close the issue... re-opening

from fake-timers.

abel-matsec avatar abel-matsec commented on August 16, 2024

Thanks for the explanation! It might be beneficial to add some notes to the documentation for the sync methods to warn folks of edge cases like this they might run into. It was very surprising and non-obvious for us, and a helpful doc caveat could have saved a lot of time.

Thanks for the great work on this lib!

from fake-timers.

benjamingr avatar benjamingr commented on August 16, 2024

Thanks for the explanation! It might be beneficial to add some notes to the documentation for the sync methods to warn folks of edge cases like this they might run into. It was very surprising and non-obvious for us, and a helpful doc caveat could have saved a lot of time.

This is more of a bluebird caveat than a fake-timers one, though I agree a note about this potential behavior may be useful. Wanna open a docs PR :)?

from fake-timers.

Related Issues (20)

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.