排队的承诺 [英] Queuing promises
问题描述
我使用 mbostock / 排队排队几个异步操作。它更多的是速率限制(UI产生几个事件,其中后端可以处理缓慢),并且还要确保它们按顺序处理。我使用它像
I use mbostock/queue for queuing few async operation. It is more to rate limit (UI generate few events, where the backend can process it slowly), and also to make sure they are processed sequentially. I use it like
function request(d, cb) {
//some async oper
add.then(function(){
cb(null, "finished ")
})
}
var addQ = queue(1);
addQ.defer(request) //called by few req at higher rates generated by UI
我已经使用了异步操作angular.js $ Q。所以,我必须用 mbostock /队列
,或者我可以建立一个出队列 $ Q
(这是在精神上 https://github.com/kriskowal/q )
I already uses angular.js $q for async operation. So, do I have to use mbostock/queue
, or can I build a queue out of $q
(which is in spirit https://github.com/kriskowal/q)
感谢。
推荐答案
简短的回答是否定的,你不需要额外的库。 Promise.then()足够原子。长的答案是:这是值得做队列()函数来保持code干燥。 蓝鸟,承诺似乎pretty完整的,但这里的基于AngularJS的$ Q东西。
The short answer is no, you don't need an extra library. Promise.then() is sufficiently "atomic". The long answer is: it's worth making a queue() function to keep code DRY. Bluebird-promises seems pretty complete, but here's something based on AngularJS's $q.
如果我正在.queue()我希望它处理的错误为好。
If I was making .queue() I'd want it to handle errors as well.
下面是一个转角服务工厂,有的使用情况:
Here's an angular service factory, and some use cases:
/**
* Simple promise factory
*/
angular.module('app').factory('P', function($q) {
var P = $q;
// Make a promise
P.then = function(obj) {
return $q.when(obj);
};
// Take a promise. Queue 'action'. On 'action' faulure, run 'error' and continue.
P.queue = function(promise, action, error) {
return promise.then(action).catch(error);
};
// Oook! Monkey patch .queue() onto a $q promise.
P.startQueue = function(obj) {
var promise = $q.when(obj);
promise.queue = function(action, error) {
return promise.then(action).catch(error);
};
return promise;
};
return P;
});
如何使用它:
.run(function($state, YouReallyNeedJustQorP, $q, P) {
// Use a $q promise. Queue actions with P
// Make a regular old promise
var myPromise = $q.when('plain old promise');
// use P to queue an action on myPromise
P.queue(myPromise, function() { return console.log('myPromise: do something clever'); });
// use P to queue an action
P.queue(myPromise, function() {
throw console.log('myPromise: do something dangerous');
}, function() {
return console.log('myPromise: risks must be taken!');
});
// use P to queue an action
P.queue(myPromise, function() { return console.log('myPromise: never quit'); });
// Same thing, but make a special promise with P
var myQueue = P.startQueue(myPromise);
// use P to queue an action
myQueue.queue(function() { return console.log('myQueue: do something clever'); });
// use P to queue an action
myQueue.queue(function() {
throw console.log('myQueue: do something hard');
}, function() {
return console.log('myQueue: hard is interesting!');
});
// use P to queue an action
myQueue.queue(function() { return console.log('myQueue: no easy days'); });
这篇关于排队的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!