0

I have a for loop that calls a function in Javascript (getItemsForFolderID) for each item in that array(subfolders.data). In this case there are 80 items in the array, but I can only see log statements for 6... getItemsForFolderID contains a promise that makes a call to a function that returns a promise and calls a REST API retrieving items (this works!)

Anyone know why only 6 calls are made? I get 80 console entries for console.log('Getting Items for: ' + subfolders.data[j].id) but only six of either console.log('No items for folder: ' + folderID); or console.log('There were items');

Here is the loop:

for (var j = 0; j < subfolders.data.length; j++) {
    console.log('Getting Items for: ' + subfolders.data[j].id);
    getItemsForFolderID(subfolders.data[j].id);
}

And the function we're calling:

function getItemsForFolderID(folderID) {
  var getItemsPromise = getItemsForFolderIDFromAPI(folderID);
  getItemsPromise.then(function(items) {
    if(items.data.length === 0) {
        console.log('No items for folder: ' + folderID);
    }
    else {
        console.log('There were items');
    }
  });
}



var RateLimiter = require('request-rate-limiter');

var limiter = new RateLimiter({rate: 100, interval: 60, backoffCode: 429, backoffTime: 15, maxWaitingTime: 2600
});

function getItemsForFolderIDFromAPI(folderID) {
    return new Promise(function(resolve, reject) {
        limiter.request({
            url: url + '/' + folderID,
            method: 'get',
            headers: headers
        }).then(function (response) {
            resolve(JSON.parse(response.body));
        }).catch(function (err) {
            reject(error);
        });
    });
}
MattTheHack
  • 1,354
  • 7
  • 28
  • 48
  • 2
    What do you see in the network tab (developer tools)? Can you tell whether 80 HTTP requests were made? What about responses? What do your server logs show? What exactly does `getItemsForFolderIDFromAPI()` look like? – Pointy Oct 03 '18 at 17:05
  • 1
    Apologies @MarkMeyer I fixed that. It was a typo – MattTheHack Oct 03 '18 at 17:12
  • @Pointy I am running this code as a nodejs app from VS Code and I'm not aware how to view outgoing HTTP Requests there. I get responses for six requests, but any more. – MattTheHack Oct 03 '18 at 17:19
  • @Pointy I added that function to help diagnose the issue – MattTheHack Oct 03 '18 at 17:22
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Oct 03 '18 at 17:29
  • may be browser blocking requests.. check this our https://stackoverflow.com/questions/985431/max-parallel-http-connections-in-a-browser – Vishal Rajole Oct 03 '18 at 17:31
  • @VishalRajole Thanks for this! Was not aware! I'm running the solution through the command line with node so how do I know what browser I'm using? Is the browser even a factor? – MattTheHack Oct 03 '18 at 17:32
  • @Bergi Is this bad practice? Could you link me to an article describing better practice? I'm interested in improving :) – MattTheHack Oct 03 '18 at 17:33
  • @MattTheHack Yes it is, and I already linked the answers that should help you to avoid it. – Bergi Oct 03 '18 at 17:34
  • 1
    @MattTheHack If you're running this in node, then you are not using a browser and Vishal's comment does not apply – Bergi Oct 03 '18 at 17:35
  • @MattTheHack I was assuming you are using browser. Anyway what node version you are using. Hope this helps https://stackoverflow.com/questions/12060869/why-is-node-js-only-processing-six-requests-at-a-time – Vishal Rajole Oct 03 '18 at 17:40

0 Answers0