Function that returns the value from updateData

  • 1
  • Question
  • Updated 3 years ago
  • Answered
what would be the proper way of coding this function:



retValue gets set in the callback from the model.updateData().  The problem seems to be that the function is returning before the callback is called, hence, returning zero.

Can we make the function wait until the callback is done before returning?  If so how would you do that?

Any thoughts?

Thank you.
Photo of Sofware Developer Guy

Sofware Developer Guy

  • 1,354 Points 1k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
updataData() is asynchronous.  

Here's a pattern that I use and you should be able to adopt it for your needs.

var deferred = $.Deferred();
...
$.when(myModel.updateData())       
   .done(function () {
           // Success      
           deferred.resolve();      
   })
   .fail(function () {
           // Error
           deferred.reject();
 }); ...
return deferred.promise();
Photo of Sofware Developer Guy

Sofware Developer Guy

  • 1,354 Points 1k badge 2x thumb
Thanks for your input:

I tried this and I still get the same issue.  You suggested to use $.Deferred() which is probably the piece I am missing.  I am not familiar with $.Deferred() so I am going to investigate.  Am I on the right track?


Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Do investigate the Deferred paradigm.  There are a number of community posts here where it is explained.