When working with a save function that requires you to call another function to retrieve the revision number and make an API call, both of which are asynchronous in nature, how can you ensure one function waits for the other to execute?
$scope.getRevision = function(callback){
Api.Product.querymyProdById({ prodId: $scope.editProdId }).$promise.then(function(result) {
if (result && result.length >= 1 && result[0].effectiveDate != $scope.editProdmyDate) {
$scope.editProdRevision = result && result[0].revision + 1;
callback();
} else {
$scope.editProdRevision = 100;
}
});
}
$scope.saveProd = function(){
$scope.getRevision(function(){});
Api.Product.save({
id: $scope.ProdId;
revision:$scope.editProdRevision
-some code
}
In the above code, I want to ensure that the save API is not called until the prodRevision is obtained.
Do you have any suggestions on how to achieve this?