簡體   English   中英

如何使用瓶頸npm模塊

[英]How to use the bottleneck npm module

我剛剛發現了這個瓶頸npm模塊,以限制每秒的請求數。 我理解了bottleneck()構造函數,但無法理解submit和schedule()方法,可能是因為我是節點中的初學者並且不了解promise。

無論如何,我找不到任何關於使用谷歌瓶頸的例子。

基本nodejs和express中的瓶頸示例可以提供很多幫助。

這是npm包: 瓶頸npm模塊

我建議首先了解承諾,並研究請求承諾。 以下是如何使用承諾從簡單的氣象服務獲取信息:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM