29e*_*9er 29 promise angularjs
在Angular $http文档中,它提到您可以将'timeout'配置设置为数字或承诺.
timeout - {number | Promise} - 超时(以毫秒为单位),或承诺在解决时应中止请求.
但我不确定如何使用承诺来完成这项工作.我如何设定数字和承诺?基本上我希望能够知道由于"超时"或其他原因而导致的http调用(promise)是否存在错误.我需要能够区分它们.谢谢你的帮助 !!!
Ila*_*mer 37
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();
}
Run Code Online (Sandbox Code Playgroud)
timeout.then(timeoutRequest) 意味着当promise被解决(不被拒绝)时,将调用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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32998 次 |
| 最近记录: |