0

I'm currently trying something in Node JS where I want a function with a callback to update a variable name and then I will log out the variable name. Below describes what I am trying to acheive

var testVariable = "first"

functionWithCallbackWhichPopulatesTestVariableWithWordSecond(callback)

Print testVariable 

In the above scenario when testVariable is output it is outputting "first" but I want it to be "second" as the callback function that is passed in should change it to "second".

Hopefully that makes sense. I'm not sure I have explained it the best but would really appreciate some help with this.

Thanks.

J145
  • 607
  • 1
  • 11
  • 16
  • I can't tell whether you've got a scoping problem (where `callback` doesn't see the right variable) or you don't understand how async works (the callback is being called after the print already happened). – btilly Apr 20 '23 at 20:34
  • 1
    You should add the actual code you're using rather than pseudocode which we can't debug. We have no idea, for example, what `callback` is doing. In it's simplest form [it works](https://jsfiddle.net/k1m27c98/). – Andy Apr 20 '23 at 20:37
  • 1
    Does either `functionWithCallbackWhichPopulatesTestVariableWithWordSecond()` or `callback()` contain asynchronous operations? – Barmar Apr 20 '23 at 20:39
  • Thank you guys, I realize my question was poorly put together and had a severe lack of useful information. I have reworded and created a JSFiddle representing the question in another post here: https://stackoverflow.com/questions/76084810/getting-a-callback-to-wait-on-an-async-call – J145 Apr 23 '23 at 12:25

0 Answers0