Skip to content

Instantly share code, notes, and snippets.

@kurtroberts kurtroberts/Bottleneck.js
Last active Aug 14, 2018

Embed
What would you like to do?
Strategies for meeting API Limits with Promises
const _ = require('lodash'),
Bottleneck = require('bottleneck'),
limiter = new Bottleneck({
minTime: 333,
maxConcurrent: 1
}),
Promise = require('bluebird'),
promises = [];
_.range(10).forEach(function (x) {
promises.push(limiter.schedule(() => console.log(x)));
});
Promise.all(promises).then(() => console.log('done'));
function getThingsAsync (options) {
//...
//returns a promise
}
function getAllThings (nextToken, allThings) {
let options = {};
if (!allThings)
allThings = [];
if (nextToken)
options.NextToken = nextToken;
return getThingsAsync(options).then(function (things) {
if (!things.NextToken)
return allThings;
else
return getAllThings(things.NextToken, allThings.concat(things.Things));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.