Skip to content

Instantly share code, notes, and snippets.

@kurtroberts
Last active September 1, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtroberts/8e0083d77e9d2e55e38db63a793f847b to your computer and use it in GitHub Desktop.
Save kurtroberts/8e0083d77e9d2e55e38db63a793f847b to your computer and use it in GitHub Desktop.
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.concat(things.Things);
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