0

I want to get the value returned by ajax call in variable x without making the ajax call synchronus. How can I achieve this?

  foo = function() {
        var x = foo1(); 
        // var x is always undefined
  }

  foo1 = function() {
      var deferred = foo2();
      deferred.then(function(response) { return response; });
  }

  foo2 = function() {
      return dojo.xhrGet(); // ajax call
  }
vinit
  • 155
  • 14
  • 2
    No matter how many functions you create and how many promises you wrap, you'll never get the value outside of the callback. – Kevin B Sep 14 '15 at 19:19
  • So, how to deal with a situation, where a function requires data from an ajax call. So, it is calling another function where the ajax call is implemented? – vinit Sep 16 '15 at 17:34
  • By either having it accept a callback, or by having it return a promise – Kevin B Sep 16 '15 at 17:37
  • Here I am returning the a deferred in foo2 . So, how can use deferred's then function to either use the response in function foo1 or return the response to function foo. – vinit Sep 16 '15 at 18:23
  • `foo = function (){ return foo1(); } ` – Kevin B Sep 16 '15 at 18:24
  • `foo1 = function() { var deferred = foo2(); return deferred.then(function(response) { return response; }); }` – Kevin B Sep 16 '15 at 18:25
  • 1
    but, all of this can be simplified to: `foo2().then(function () { /* all done!*/ });` the rest is redundant. – Kevin B Sep 16 '15 at 18:25

0 Answers0