angularjs – Angular $http:在’timeout’配置上设置一个承诺
发布时间:2020-12-17 08:25:49 所属栏目:安全 来源:网络整理
导读:在角度 $http docs中,它提到您可以将“超时”配置设置为数字或承诺。 timeout – {number|Promise} – timeout in milliseconds,or promise that should abort the request when resolved. 但我不知道如何使用承诺使这项工作。我如何设定一个数字和承诺?
在角度
$http docs中,它提到您可以将“超时”配置设置为数字或承诺。
但我不知道如何使用承诺使这项工作。我如何设定一个数字和承诺?
这段代码是从
$httpBackend source code:
if (timeout > 0) { var timeoutId = $browserDefer(timeoutRequest,timeout); } else if (timeout && timeout.then) { timeout.then(timeoutRequest); } function timeoutRequest() { status = ABORTED; jsonpDone && jsonpDone(); xhr && xhr.abort(); } timeout.then(timeoutRequest)意味着当承诺得到解决(不被拒绝)时,将调用timeoutRequest并终止xhr请求。 如果请求超时,则reject.status === 0(注意:如果网络发生故障,则reject.status也将等于0),一个示例: app.run(function($http,$q,$timeout){ var deferred = $q.defer(); $http.get('/path/to/api',{ timeout: deferred.promise }) .then(function(){ // success handler },function(reject){ // error handler if(reject.status === 0) { // $http timeout } else { // response error status from server } }); $timeout(function() { deferred.resolve(); // this aborts the request! },1000); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |