jquery - How to get value from function one with deferred object into function two? -


i've been able implement deferred object, trying access value first function within second function.

i'm not sure if i'm not understanding should returning in functionone(), or if deferred objects aren't designed work in way.

how access results functionone()'s getjson() in second function?

function functionone(x, y) {     var r = $.deferred();     $.getjson("/my_path", {         k1: x,         k2: y,         format: 'json'     }, function (results) {      });      settimeout(function () {         r.resolve();     }, 2500);      return r; }  $(document).ready(function () {     functionone(x, y).done(function (results) {         // how access keys functionone's getjson results?         var myvar1 = results.great_key_1;         var myvar2 = results.great_key_2;     }); }); 

just return promise returned $.ajax

function functionone(x, y) {     return $.getjson("/my_path", {         k1: x,         k2: y,         format: 'json'     }); } 

your version bit of anti-pattern see work need pass in ajax results resolve(). showing since have delay implemented , able see full effect of delay. otherwise should use first version

function functionone(x, y) {     var r = $.deferred();     $.getjson("/my_path", {         k1: x,         k2: y,         format: 'json'     }, function (results) {          settimeout(function () {             r.resolve(results);// resolve data          }, 2500);     });      return r.promise; } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -