Eugene Goldberg Eugene Goldberg - 7 months ago 47
Javascript Question

how to properly handle promises?

In my Angular app, I'm using $odataresource for data retrieval and updates.

I have the following lines:

var measure = $odataresource("http://windows-10:8888/ChangeMeasure/");

var myMeasure = measure.odata().filter("ID",$scope.ngData[index].ID)
.single();


I, then, need to issue an update on myMeasure:

myMeasure.$update();


The trouble is - when I issue $update, myMeasure is still an unresolved promise.

What is the proper syntax to call
myMeasure.$update();
when
myMeasure
promise is resolved?

Answer

Looking at the documentation of ODataAngularResources try the following:

  var measure = $odataresource("http://windows-10:8888/ChangeMeasure/");
  var myMeasure = measure.odata().filter("ID",$scope.ngData[index].ID)
                  .query(function(data){
                    data[0].$update(); 
                    console.log("Everything went ok!")
                   },function(){
                    console.log("Oops, something wrong happened!")
                   });