Skip to content

Instantly share code, notes, and snippets.

@johncmunson
Created September 27, 2019 16:09
Show Gist options
  • Save johncmunson/90f9abbdf0a809b32928a0e010b284ba to your computer and use it in GitHub Desktop.
Save johncmunson/90f9abbdf0a809b32928a0e010b284ba to your computer and use it in GitHub Desktop.
Throttling concurrent API calls
const pMap = require('p-map')
const Chance = require('chance')
const chance = new Chance()
const userIds = [ 52, 84, 71, 66, 12, 39, 18, 99, 7, 48 ]
// Simulate a network call
const getUser = async (id) => {
await new Promise(resolve => setTimeout(resolve, 1000))
return chance.name()
}
const getUsers = async (userIds) => {
const users = await pMap(userIds, getUser)
// Using pMap without concurrency is equivalent to...
// const users = await Promise.all(userIds.map(id => getUser(id)))
return users
}
const getUsersThrottled = async (userIds) => {
const users = await pMap(userIds, getUser, { concurrency: 2 })
return users
}
(async () => {
console.time('getUsers')
await getUsers(userIds)
console.timeEnd('getUsers') // => Roughly 1s
console.time('getUsersThrottled')
await getUsersThrottled(userIds)
console.timeEnd('getUsersThrottled') // Roughly 5s
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment