创建具有蓝鸟承诺的节流功能
Creating throttling function with Bluebird promises
我正在尝试创建一个限制函数。我查看了一些SO帖子并复制了一些代码,但我无法延迟它。
基本上,我在一个类中有许多方法需要调用Amazon API。它们都使用了一个通用函数 - doCall
我实现如下:
Amazon.prototype.doCall = function(func) {
var self = this;
var queue = P.resolve();
function throttle(fn) {
var res = queue.then(function() { // wait for queue
return fn(); // call the function
});
queue = P.delay(61000).return(queue); // make the queue wait for 61 seconds
return res; // return the result
}
// Create instance of MWS client
if (!this.client) {
this.client = new mws.Client(key, secret, merchant, {});
}
var call = function() {
// The library uses a weird signature so I am wrapping it thus
return new P(function(resolve, reject) {
// The original MWS library call
self.client.invoke(func, function(r, e) {
// ... stuff
resolve(r);
});
});
};
return throttle(call);
};
基本上我获取订单列表和订单,需要延迟每次调用 60+ 秒。现在,这一切都毫不拖延地发生。建议?
我基本上是这样使用它的(人为的,但应该给出这个想法)
self.doCall(ListOrders).then(function(res) {
// parse results
self.doCall(ListMoreOrdersByPage).then(function(res) {
// Now I might go through each and fetch details
var ids = [...] // Parse result for ids
return P.map(ids, function(id) {
return doCall(GetOrderById);
});
....
你的问题是
Amazon.prototype.doCall = function(func) { var queue = P.resolve(); …
意味着您在该方法的每次调用上重新创建一个新queue
。不是很有帮助。相反,您可能希望每个Amazon
一个队列,因此将初始化放在构造函数中。
我还简化了您的代码:
function Amazon(…) {
…
this.queue = P.resolve();
}
Amazon.prototype.doCall = function(func) {
if (!this.client) {
// Create instance of MWS client
this.client = new mws.Client(key, secret, merchant, {});
}
var self = this;
var res = this.queue.then(function() {
// The library uses a weird signature so I am wrapping it thus
return new P(function(resolve, reject) {
self.client.invoke(func, function(r, e) {
// ... stuff
resolve(r);
});
});
});
this.queue = this.queue.delay(610000); // make the queue wait for 61s
// if you want to make it wait *between* calls, use
// this.queue = res.catch(function(){}).delay(610000);
return res;
};
我想
在创建一个实际的工作解决方案后再回来。让我搞砸的技巧是你需要将未调用的函数传递到队列器中。我知道这不是 OP 所要求的,但我希望这可以帮助其他寻找可扩展解决方案的人。
你可以在这里看到它的实际效果:http://jsbin.com/seqeqecate/5/
function asyncFunction(){
var deferred = Promise.defer();
setTimeout(function(){
console.log('ping');
deferred.resolve();
},3000);
return deferred.promise;
}
function AsyncQueuer(){
var queue = [],
running = false;
function runQueue(){
var first = queue.shift();
running = true;
if (first) {
first.promisable.apply(first.context).then(function() {
first.deferral.resolve();
runQueue();
},
function() {
first.deferral.reject();
runQueue();
});
} else {
running = false;
}
}
return {
add: function(promisable, context) {
var deferred = Promise.defer();
queue.push({
promisable: promisable,
deferral: deferred,
context: context || window
});
if (!running) {
runQueue();
}
return deferred;
}
};
}
var asyncQueuer = new AsyncQueuer();
asyncQueuer.add(asyncFunction);
asyncQueuer.add(asyncFunction);
asyncQueuer.add(asyncFunction).then(function(){}).fail(function(){});
相关文章:
- 添加文字和评论功能更新Div
- 为什么同步睡眠功能没有被承诺内异步化
- 创建具有蓝鸟承诺的节流功能
- 承诺解除仍在履行功能
- AngularJS ng-init指令被分配了功能和承诺
- JS承诺在功能内实现
- 茉莉花测试一个承诺,然后功能
- Q.all 块中的所有功能都没有被承诺
- 承诺和Q - 使用forEach完成一长串的功能
- 异常捕获在功能上是否与承诺错误回调相同
- Do承诺会扰乱Parse云函数的功能
- 传递'这'变成'那么'承诺的功能
- jquery配置错误:readyList.承诺不是一种功能
- 功能上的多个承诺
- 当我不使用“then”时,有什么(负面)副作用吗?承诺的功能
- 链接功能作为承诺
- 使用jQuery的功能承诺
- 拖延者-当功能失效时返回未决承诺
- 在功能上承诺拒绝
- 对承诺的功能操作(例如筛选)