0

I have a simple function that is consoled as such:

console.log("the state found was: " + findState());

The problem I am having is returning the found state when my findState() func is initialized. It shows fine in the console but will not return to the original initialization of findState() in the consoled output above.

I would like to use findState() in a string.

Calling the findState function

I have a javascript function with a very simple ajax request that pulls in a set json file. The json data comes back perfectly.

var foundStnew = "California";

var findState = function(){

     $.ajax({
          url: 'usstates.json',
          type: 'get',
          dataType: 'json',
          success: function(data){
               $(data.usstates).each(function(i,v){
                    if (v.abbreviation === foundStnew){
                         foundState(v.name);
                    }
               });
          }
     });

}

function foundState(state){
     var f = state;
     console.log("here is the state: " + f);
     return f
}

Like stated earlier, I can see the consoled result from inside the initiating functions but calling it from the originating function won't work.

How would I get the result of the function back. It always comes back undefined when i pass a value by calling findState();

Please help!

MizAkita
  • 1,115
  • 5
  • 21
  • 52
  • Short answer: *"You can't."*. You need to use the result in the place where you received it. In your case, you're passing it to the `foundState()` function, so you can work with it there. If `findState` is meant to be reused for various situations, then change it to receive a callback function as an argument, and then invoke that. So like `findState(foundState)` and ... `var findState = function(callback) {...` and then ... `callback(v.name);` –  May 22 '16 at 15:26
  • Check that you're setting your "foundStnew" global variable first. It may be better to bundle your call into an object, and use ajax context option, instead of using globals, but the global should work too. – William Walseth May 22 '16 at 15:28
  • @squint i am trying your method of adding callback. does this go into the success handler? or outside of ajax method inthe findState() method? i tried it but i get 'v' is undefined. – MizAkita May 22 '16 at 15:45
  • It is defined as a parameter to the `findState` function, and is called in the same spot where you're currently calling `foundState()`. So basically, it's the same thing, but instead of `foundState` being hard-coded inside `success:`, it is being passed in and invoked. But the main thing to understand is that the `return` statement will be of no use to you. You simply can not return the value to the original call site. Instead, you need to pass the dependent code *to* the place where the result is appearing. –  May 22 '16 at 16:24
  • Here's [a demo](https://jsfiddle.net/dp9zja3h/) that shows how it works. Reading through the duplicate question and answer would be a good idea too. –  May 22 '16 at 16:31
  • thanks @squint i tried this and i do get the consoled loggings up until myCallback but trying to read it with findState(myCallback); returns undefined as the other way's i've tried. – MizAkita May 22 '16 at 16:45
  • @MizAkita: `findState()` will *always* return undefined. You may be overlooking the most important point. You simply *can not* return the value from `findState()`. You need to restructure your code in such a way that you pass the dependent code *to* the result instead of having the result be delivered to the dependent code. –  May 22 '16 at 16:48

0 Answers0