admin管理员组文章数量:1291221
I have a service to for API call as following,
getValue: function(input) {
var deferred, url;
deferred = $q.defer();
url = "url";
$http.post(url, input).success(function(data, status, headers, config) {
return deferred.resolve({
success: true,
data: data,
status: status,
headers: headers,
config: config
});
}).error(function(data, status, headers, config) {
return deferred.resolve({
success: false,
data: data,
status: status,
headers: headers,
config: config
});
});
return deferred.promise;
}
But this is async. How can I convert it to sync(I want to make it wait till I get the result)?
I have a service to for API call as following,
getValue: function(input) {
var deferred, url;
deferred = $q.defer();
url = "url";
$http.post(url, input).success(function(data, status, headers, config) {
return deferred.resolve({
success: true,
data: data,
status: status,
headers: headers,
config: config
});
}).error(function(data, status, headers, config) {
return deferred.resolve({
success: false,
data: data,
status: status,
headers: headers,
config: config
});
});
return deferred.promise;
}
But this is async. How can I convert it to sync(I want to make it wait till I get the result)?
Share Improve this question edited Oct 28, 2014 at 8:17 Mehdi Haghgoo 3,4848 gold badges56 silver badges100 bronze badges asked Oct 28, 2014 at 8:13 Erma IsabelErma Isabel 5552 gold badges11 silver badges25 bronze badges 3-
1
This is what promises are for. Performe your action using the promise you return from the function. e.g the
deffered.promise
you return.then( function () { /* your action */ })
. Or, in your case (since it basically resolves to the http response, performe your action in the post fn). – haki Commented Oct 28, 2014 at 8:15 - 1 I think it not possible to do it sync, BUT you using promise which allows you to have control of it, and you can tell your app wait until it will resolve/reject – Narek Mamikonyan Commented Oct 28, 2014 at 8:21
- Possible duplicate of How to $http Synchronous call with AngularJS – Andre Figueiredo Commented Oct 2, 2015 at 14:15
1 Answer
Reset to default 6No that is not possible with Angular.
See https://github./angular/angular.js/blob/master/src/ng/httpBackend.js#L51 where the XMLHttpRequest is opened with
xhr.open(method, url, true);
The third parameter in an xhr.open()
can be set to false
or true
, where false is synchronous and true is asynchronous. In the Angular case, it is hardcoded to true
, so that all outgoing calls will be asynchronous.
Use the .success()
callback to wait until the async call returns, and then do whatever you want to do there.
As per the suggestion in the ments, you can of course also do the calls via raw javascript, jQuery or any other library that supports synchronous calls, but I would advise using callbacks/defers with the asynchronous angular call, because synchronous calls are blocking and blocking is bad.
本文标签: javascriptAngularJs http Synchronous callStack Overflow
版权声明:本文标题:javascript - AngularJs: $http Synchronous call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741526937a2383524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论