-2

Hello before you flag this question as duplicate please read it, I cannot find an example that I personally understand for my specific problem.

here is a pseudo code function:

foo(a1,a2,a3){
    //do some logic here
   return data
   //lets say the data = 15
    }

foo2(){
    //do some stuff
    data = foo(a1,a2,a3)
    console.log(data)
    }

And as you probably know, the data printed is undefined, not 15 as I want. Can someone please just give me an example for this scenario. I have been trying to understand this for hours. I do not need a long explaination.

recurf
  • 237
  • 4
  • 16
  • 1
    example code looks like it should work... it's also not asynchronous. you really don't have enough here to demonstrate what the issue is. – john_omalley Jan 19 '17 at 20:55
  • I am guessing you are asking something like this: https://jsfiddle.net/rq936qtn/ – yBrodsky Jan 19 '17 at 20:56
  • @yBrodsky I have seen that one before, where do I put my parameters??? – recurf Jan 19 '17 at 20:59
  • @john_omalley I posted a similar question with my actual code, and it got instantly locked and down voted. Its difficult to actually get help on here. – recurf Jan 19 '17 at 21:01
  • In your actual code, is the logic performed in foo() async? Does it have, say, an internal callback somewhere that actually sets the value of `data`? – Fissure King Jan 19 '17 at 21:07
  • @FissureKing Yes it is async, I am running a visual regression algorithm and saving some images generated to the hard drive. the function returns the data from the visual regression test. in foo2 im trying to use that data. – recurf Jan 19 '17 at 21:08
  • 1
    @recurf Ok, you have a couple of options in that case. I'll write something out in an answer below. – Fissure King Jan 19 '17 at 21:10
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – idbehold Jan 19 '17 at 21:38
  • this has got me banned from stack over flow for god sake – recurf Jan 21 '17 at 18:49

2 Answers2

0

This works for me:

foo = function(a1,a2,a3) {
    //do some logic here
    var data = a1 + a2 + a3;
    return data;
    //lets say the data = 15
    }

foo2 = function() {
    //do some stuff
    var data2 = foo(5,5,5);
    console.log(data2);
    return data2
    }

foo2();

It logs 15 to the console, as well as returning 15...

No one is sure exactly what issue you're having since your pseudocode is a little vague, but I think you'll benefit from reading about hoisting here - https://www.interviewcake.com/question/python/js-scope. I'm not going to copy paste the answer from the website because it is formatted so much better there. :)

0

Let's first rewrite your pseudo-code to reflect the async nature of the logic contained within the first function:

foo(a1,a2,a3){
  //do some *async* logic here
  regressionFunction(a1, a2, a3, function(result) {
    data = result;
  });
  
  return data;
  //lets say the data = 15
}

foo2(){
  //do some stuff
  data = foo(a1,a2,a3)
  console.log(data)
}

This will result in data being undefined in foo2 because data has not yet been set when foo returns. Hence, async.

There are several ways to deal with this, but the simplest at this point is probably to use a Promise. Think of a Promise as a placeholder for a guaranteed future value, which we can then instruct our code to wait upon before proceeding.

Instead of simply returning data in foo, return a Promise to resolve when data is ready:

foo = function(a1,a2,a3) {
  return new Promise(function(resolve, reject) {
    regressionFunction(a1, a2, a3, function(result) {
      resolve(result);
    });
  });
}

And then instead of expecting the result of foo to be immediately available in foo2, we use the Promise instance's then method.

foo2 = function() {
  //do some stuff
  var data2 = foo(5,5,5);
  data2.then(function(result) {
    console.log('Finally got a result!', result);
  });
}

If you need to return a value from foo2 dependent upon the result of foo, of course, you would need to return a Promise there as well. Conveniently, the the return type of a .then method is itself a Promise so we can simply return it:

foo2 = function() {
  //do some stuff
  var data2 = foo(5,5,5);
  return data2.then(function(result) {
    console.log('Any function receiving this as input can access the result now by accessing the `.then` method of this function');
    return result;
  });
}

This is a simplification of Promises, but should get you started.

Fissure King
  • 1,250
  • 7
  • 17
  • I implemented this exactly, and now it just ignores the bit inside the Promise function. This Syntax is just god awful, really driving me mad lol. – recurf Jan 21 '17 at 17:24
  • Try adding some console logging to get at exactly where it's falling apart and check the inner function signature to ensure your callback is going to be respected. Also chain a catch function onto the `then` in foo2, e.g., `data2.then(. . .).catch(function(err) { console.log('Error returned from promise', err) })` – Fissure King Jan 22 '17 at 02:44