Last active
September 1, 2020 19:44
-
-
Save kurtroberts/8e0083d77e9d2e55e38db63a793f847b to your computer and use it in GitHub Desktop.
Strategies for meeting API Limits with Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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