0

I want to get the result returned where I call the function. How can I do this? I get the result through the console.log(result) but not the console.log(finger).

I want to pass it outside.

function fingercode() {
    var fp = new Fingerprint2();
    fp.get(function(result) {
      console.log(result);
    });
}

var finger = fingercode();
console.log(finger);
ches
  • 6,382
  • 2
  • 35
  • 32
villoui
  • 245
  • 1
  • 3
  • 11
  • what does `fp.get` actually do? You can't return a deffered function because it's execution is deffered . You'll need some kind of callback – Liam May 05 '16 at 12:51
  • Is it safe to assume that `fp.get` does some form of ajax get/load? If so, just search SO for the 100s of duplicate questions. – freedomn-m May 05 '16 at 12:53
  • 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) – Liam May 05 '16 at 12:55
  • or [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Liam May 05 '16 at 12:56

1 Answers1

3

You need the callback function

function fingercode(callback) {
      var fp = new Fingerprint2();
      fp.get(callback);
}

fingercode(function(finger){
    console.log(finger);
});
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51