jquery - Deferred functions and making sure final function receives both data -
i function displayresults() data before renders value. functions getresulta() , getresultb() using $.deferred. function getresultb() dependent on results getresulta().
function getresulta() { var deferred = $.deferred(); . . . . deferred.resolve(somevalue); return deferred.promise(); } function getresultb() { var deferred = $.deferred(); . . getresulta().done(function(somevalue) { deferred.resolve(somevalue); return deferred.promise(); }) } function displayresults() { getresultb().done(function(response) { // display response }) }
is code correct?
your getresultb()
promise anti-pattern. have promise returned getresulta()
. don't need create new one. can return 1 have.
function getresultb() { . . return getresulta().then(function(somevalue) { // whatever code want here // code here can return new value, return value have // after doing other operations or can return new promise // added chain return somevalue; }); }
you can use getresultb()
how planning:
function displayresults() { getresultb().then(function(response) { // display response }) }
note: switched use .then()
instead of .done()
because es6 standards way of coding promises (and works jquery too). so, jquery moves more consistent promise standards or code promises sources other jquery, won't have change code or style.
Comments
Post a Comment