[英]How to use the bottleneck npm module
I just found out about this bottleneck npm module to limit the no of requests per second. 我刚刚发现了这个瓶颈npm模块,以限制每秒的请求数。 I understood the bottleneck() constructor, but cannot understand the submit and schedule() methods, probably because I am a beginner in node and don't know about promise.
我理解了bottleneck()构造函数,但无法理解submit和schedule()方法,可能是因为我是节点中的初学者并且不了解promise。
Anyway, I couldn't find any examples about using bottleneck from google. 无论如何,我找不到任何关于使用谷歌瓶颈的例子。
A bottleneck example in basic nodejs and express could help a lot. 基本nodejs和express中的瓶颈示例可以提供很多帮助。
Here is the npm package: bottleneck npm module 这是npm包: 瓶颈npm模块
I'd recommend learning about promises first, and look into request-promise. 我建议首先了解承诺,并研究请求承诺。 Here is how you could use it with promises to get info from a simple weather service:
以下是如何使用承诺从简单的气象服务获取信息:
var rp = require("request-promise");
var Bottleneck = require("bottleneck");
// Restrict us to one request per second
var limiter = new Bottleneck(1, 1000);
var locations = ["London","Paris","Rome","New York","Cairo"];
// fire off requests for all locations
Promise.all(locations.map(function (location) {
// set up our request
var options = {
uri: 'https://weatherwebsite.com?location=' + location,
json: true
};
// run the api call. If we weren't using bottleneck, this line would have just been
// return rp(options)
// .then(function (response) {...
//
return limiter.schedule(rp,options)
.then(function (response) {
console.log('Weather data is', response);
})
.catch(function (err) {
// API call failed...
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.