在 $http 调用中使用 $q 和 deferred.notify
Using $q and deferred.notify with $http calls
我正在尝试构建 $http.get
请求的数组,然后通过它们承诺 运行,并在此过程中获得进度通知。
我大致了解如何执行此操作,但出于某种原因,我最终只能同时进行和返回所有 20 个 HTTP 调用。通知永远不会 运行.
基本上是为了和数组 ['Thing1', 'Thing2', 'Thing3', ...]
我想提出一个 $http.get 请求。在执行这个 $http 调用数组时,我想获得当前进度(1/3 完成),直到所有完成,然后 运行 一个函数。
感谢任何帮助,谢谢!
根据文档
then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available
所以基本上让我们有一堆承诺
var req1 = $http.get('url1');
var req2 = $http.get('url2');
var req3 = $http.get('url3');
var req4 = $http.get('url4');
//do stuff when all of them are resolved
var promiseArray = [req1, req2, req3, req4];
$q.all(promiseArray).then(function(successResponse){ //success callback - it's ok
//do stuff with my success execution
}, function(errorResponse){ //error callback - it has errors
//throw exception
}, function(notificationResponse){ //notification callback - do stuff while executing
//notify my stuff execution
});
就是这样。简单到死
正如@jusopi 的评论,值得注意的是,每次响应获得成功结果时都会调用最后一个回调(通知回调)。
Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
如需进一步阅读,请参阅此 link
伪代码
flags = 0
rqsts = [
$http.get(url1)
.then (rsp)->
flags++
rsp.data.data
.catch (err)->
# key here is to NOT reject but pass a successful failure
# otherwise $q.all will bomb out
err
.....
$http.get(urlN)
.then (rsp)->
flags++
rsp.data.data
.catch (err)->
# key here is to NOT reject but pass a successful failure
# otherwise $q.all will bomb out
err
]
$q.all(rqsts)
然后在您的 UI 中,您可以查看针对 rqsts.length 的标志,看看有多少已经完成。
编辑
我添加了一个示例来展示您如何 resolve/reject 您的个人请求可以在一些请求仍处于待处理状态时增加计数器。它在 coffeescript & jade \ö/
codepen - http://codepen.io/jusopi/pen/admNmm?editors=101
说清楚
在各种评论和其他答案中指出:
As comment by @jusopi, it's worth noting that last callback (notification callback) will be called everytime a response gets success result.
Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
这仅适用于承诺的单个实例。在问题的上下文中,此通知只会在所有 服务器请求已解决后触发。它不会为每个服务器请求触发通知,因为这是所述服务器请求的责任。
这是相关的 question/answer -
我正在尝试构建 $http.get
请求的数组,然后通过它们承诺 运行,并在此过程中获得进度通知。
我大致了解如何执行此操作,但出于某种原因,我最终只能同时进行和返回所有 20 个 HTTP 调用。通知永远不会 运行.
基本上是为了和数组 ['Thing1', 'Thing2', 'Thing3', ...]
我想提出一个 $http.get 请求。在执行这个 $http 调用数组时,我想获得当前进度(1/3 完成),直到所有完成,然后 运行 一个函数。
感谢任何帮助,谢谢!
根据文档
then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available
所以基本上让我们有一堆承诺
var req1 = $http.get('url1');
var req2 = $http.get('url2');
var req3 = $http.get('url3');
var req4 = $http.get('url4');
//do stuff when all of them are resolved
var promiseArray = [req1, req2, req3, req4];
$q.all(promiseArray).then(function(successResponse){ //success callback - it's ok
//do stuff with my success execution
}, function(errorResponse){ //error callback - it has errors
//throw exception
}, function(notificationResponse){ //notification callback - do stuff while executing
//notify my stuff execution
});
就是这样。简单到死
正如@jusopi 的评论,值得注意的是,每次响应获得成功结果时都会调用最后一个回调(通知回调)。
Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
如需进一步阅读,请参阅此 link
伪代码
flags = 0
rqsts = [
$http.get(url1)
.then (rsp)->
flags++
rsp.data.data
.catch (err)->
# key here is to NOT reject but pass a successful failure
# otherwise $q.all will bomb out
err
.....
$http.get(urlN)
.then (rsp)->
flags++
rsp.data.data
.catch (err)->
# key here is to NOT reject but pass a successful failure
# otherwise $q.all will bomb out
err
]
$q.all(rqsts)
然后在您的 UI 中,您可以查看针对 rqsts.length 的标志,看看有多少已经完成。
编辑
我添加了一个示例来展示您如何 resolve/reject 您的个人请求可以在一些请求仍处于待处理状态时增加计数器。它在 coffeescript & jade \ö/
codepen - http://codepen.io/jusopi/pen/admNmm?editors=101
说清楚
在各种评论和其他答案中指出:
As comment by @jusopi, it's worth noting that last callback (notification callback) will be called everytime a response gets success result.
Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
这仅适用于承诺的单个实例。在问题的上下文中,此通知只会在所有 服务器请求已解决后触发。它不会为每个服务器请求触发通知,因为这是所述服务器请求的责任。
这是相关的 question/answer -