Browser can process scripts before handle post request. Conside I have the following sample
if (some true condition) {
console.log("ready to post")
restangular.all.post(RequestData).then(function(response){
//some post methods
console.log("response")
});
}
//then containing scripts
cosole.log('Hello')
....
ready to post Hello response
I expect to do the POST request before printing "Hello". How to overcome this?
In order to achieve what you want you should look into angularJS promises, since the POST request is asynchronous. For example check this link: http://www.webdeveasy.com/javascript-promises-and-angularjs-q-service/
The main idea is to first create a call that returns a deferred object, something like
this.myFunction = function (myForm) {
var deferred = $q.defer();
$http.post(myURL, myForm)
.success(function (data) {
//resolve the promise
deferred.resolve('SUCCESS');
})
.error(function (data) {
//reject the promise
deferred.reject('ERROR');
});
//return the promise
return deferred.promise;
}
and then call it like
var myPromise = this.myFunction ($scope.modalForm);
// wait until the promise return resolve or eject
//"then" has 2 functions (resolveFunction, rejectFunction)
myPromise.then(function(resolve){
// do stuff here, the post request is successfully finished
}, function(reject){
return;
});
Alternatively, any code you want to be executed after the POST request, you can put it in the success function of the request.