How do you return from two functions in JavaScript

The setup looks like this:

var blah = function () { var check = false; verify({ success: function () { if... check = true; else... check = false; }}); return check; }; 

The idea is to use the check function to check something, and then return true or false . However, the above method always returns false - return before calling the success function.

How do I get the results I need?

+4
source share
3 answers

You can not,

 var blah = function (callback) { verify({ success: function () { if () { callback(true); } else { callback(false); } } }); }; blah(function(result) { // my code here }); 

JQuery 1.6 has a new api promise that allows you to return an asynchronous call and make it a little more synchronous, but basically it is the same as here.

+2
source

It looks like verify asynchronously calls a success function?

If so, you need to do what you are going to do after blah as a function - name it foo . Then in verify call foo - for example:

  if... { foo(true); } else...{ foo(false); } 
+1
source

In your example, you can do something like this:

 var blah = function () { var check; try{ verify({ success: function () { if... throw true; else... throw false; }}); } catch(e){ check = e; } return check; } 
0
source

All Articles