Function that returns the value from updateData

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.

updataData() is asynchronous.  

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

var deferred = $.Deferred();<br>...<br>$.when(myModel.updateData())&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp;.done(function () {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Success &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deferred.resolve(); &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp;})<br>&nbsp; &nbsp;.fail(function () {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Error<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deferred.reject();<br>&nbsp;}); ...<br>return deferred.promise();





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?

Do investigate the Deferred paradigm.  There are a number of community posts here where it is explained.