Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the isBefore method is nondeterministic #4536

Closed
ispacowanguitar opened this issue Apr 6, 2018 · 5 comments
Closed

the isBefore method is nondeterministic #4536

ispacowanguitar opened this issue Apr 6, 2018 · 5 comments
Labels

Comments

@ispacowanguitar
Copy link

I found this while working on a calendar that needs to disable certain date ranges. It is sometimes off by one.

To reproduce:

  1. open chrome dev-tools, type in moment().isBefore(moment())
    Run it over and over again, it should always be false, but sometimes shows true. This does not appear to happen with the isAfter method. Below is a screenshot.

image

@battaile
Copy link

battaile commented Apr 8, 2018

It looks like isBefore is behaving correctly but somehow the two moment() objects are sometimes created with the second one having the earlier date. This is the check that's returning disparate results:
https://github.com/moment/moment/blob/develop/src/lib/moment/compare.js#L26

@marwahaha marwahaha added the Bug label Apr 9, 2018
@ispacowanguitar
Copy link
Author

I see, thanks for looking into it and getting back to me!

@marwahaha
Copy link
Member

I think it depends on the exact millisecond the moments are created. The fact that the second is ever created after the first feels like a browser bug.

There is a more extensive conversation around this topic here: #2697

@ashsearle
Copy link
Contributor

ashsearle commented Apr 16, 2018

The fact that the second is ever created after the first feels like a browser bug.

Looks fine to me.

Compare moment().isBefore(moment()) with:
somethingThrowingAnError().method(somethingWithSideEffects())
Or even:
somethingReturningNull().method(somethingWithSideEffects())

You wouldn't expect or want somethingWithSideEffects() to be called when an error is thrown / the method can't possibly be called.

All that to say: first must be called before second:
first().method(second())

@ichernev
Copy link
Contributor

@ispacowanguitar some of the time the moments are created in the same millisecond, some of the time not. Nothing to see here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants