0

Sorry if the title is hard to understand, because it's definitely a weird problem.

I think this screenshot best explains what's going on, and it makes no sense to me in every sense of programming. Regardless of whether a PL is interpreted or compiled, I would expect two lines within the same function to evaluate in consecutive order.

Not in this case, however:

enter image description here

You can clearly see in this side-by-side screenshot that the line I would have expected to print after the first line (consecutively speaking) actually printed/evaluated first.

This is a problem for me because the entire reason I am calling the index.getObject method within the React component constructor is to use the response in the object itself. So now all the React components are just using destName = null.

Can anyone explain this behavior? (bonus points if there's a way to make sure the first line evaluates first)

additional picture to explain behavior:

enter image description here

notacorn
  • 3,526
  • 4
  • 30
  • 60
  • Maybe it has to do with the Hot Module Replacement (HMR) interrupting the normal execution of the JavaScript? – Robert Cooper May 26 '19 at 04:13
  • Also, your logged values for `null` might be because your `redirectId` isn't a valid value on the first renders of your `NewButton` component. – Robert Cooper May 26 '19 at 04:16
  • I dont get why/how the button component could possibly be rendered without the affiliated constructor that specifies the `redirectId` @RobertCooper – notacorn May 26 '19 at 04:21
  • What happens when you log out the value of `redirectId` just before your call to `index.getObject...`? – Robert Cooper May 26 '19 at 04:23
  • 2
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Randy Casburn May 26 '19 at 04:26
  • updated the question with a picture @RobertCooper, but the redir ID's dont seem to be null – notacorn May 26 '19 at 04:27
  • Yes, as @RandyCasburn has eluded to, this is likely caused by the `index.getObject` call being asynchronous. That means the code continues to execute even though the result of `index.getObject` hasn't been returned. – Robert Cooper May 26 '19 at 04:31
  • Great thanks, @RandyCasburn or cooper I am happy to credit one of you with figuring it out – notacorn May 26 '19 at 04:39

1 Answers1

0

As @RandyCasburn has alluded to, this appears to be an issue with your index.getObject method being an asynchronous JavaScript call. This means that when calling index.getObject, the JavaScript continuous to execute code while it is waiting on the result of the index.getObject call.

Here's the canonical answer for asynchronous JavaScript questions on Stack Overflow: Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

Robert Cooper
  • 2,160
  • 1
  • 9
  • 22