3

I want custom sync and async in angularjs like below.

  var async = function (url, data, action, callback) {   
        $.ajax({
            url: global.baseURL + url,
            type: action,
            data: data,                
            async: true,//or false
            success: function (data) {
                if (data !== undefined && data !== null) {
                    callback(data);
                }
            }
        });
    }; 

In AngularJs look like this code

 $http({ method: 'GET', url: baseURL + 'Api/MobilePref/Get/'+uid
        })
        .success(function (data, status, headers, config) {
        })
        .error(function (data, status, headers, config) {
            //TODO: handl error.
        });

Can any one tell me how to setup sync and async call in angularjs?

3
  • Seems like it is not possible. AngularJs: $http Synchronous call Commented Jan 21, 2015 at 11:41
  • Synchronous calls are not possible in the $http. The backend sets async by default. Synchronous calls cause blocking and blocking is bad! Commented Jan 21, 2015 at 11:44
  • can any one give an idea to resolve it? Commented Jan 21, 2015 at 11:56

1 Answer 1

2

You can do this:

function callAjax(url, callback){
    var xmlhttp;

    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, true); // the flag true tell if this is async or not and then you can call $scope.$apply for angular to know.
    xmlhttp.send();
}
Sign up to request clarification or add additional context in comments.

2 Comments

xmlhttp.open("GET", url, false); //That means we can say It is a "sync" call?
No, set to true is sync call

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.