Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show concurrency limits & timeout example #8

Open
justsml opened this issue May 11, 2017 · 0 comments
Open

Show concurrency limits & timeout example #8

justsml opened this issue May 11, 2017 · 0 comments
Labels

Comments

@justsml
Copy link
Owner

justsml commented May 11, 2017

To prevent prematurely creating Promises and exhausting resurces, we'll start our Promise chain by simply passing our parent function's params (ids array) into Bluebird .map handler calling our api method - now with sane limits:

// In order to avoid overwhelming `api.getById` - for example lets assume we are limited to 4 concurrent connections we can easily add or remove this kind of real-world requirement 
const getUsersById = (ids = []) => Promise.resolve(ids)
  .map(api.getById, {concurrency: 4})

// Call getUsers, but attach logic to extract names out
const userNames = getUsersById([1, 2, 3])
  .all().spread((users) => users.map(u => u.name))
userNames.then(console.log)) // > ['Emily', 'Mel', 'Paris']

// Or reuse Promise w/ built-in memoization. 
//     +1 for caching lookups + disposing of objects in RAM using JS scope & garbage collection. 
const getUserNamesAndActivity = (userIds) => {
  const users = getUsersById(userIds)
  const userActivity = users.map(({username}) => api.getTopActivity({username}), {concurrency: 4}) // throttle net-bound API calls
  const userNames = users.map(({name}) => name) // no I/O or net-bound call: don't throttle
  return Promise.props({userNames, userActivity});
}

This "requirement" has a funny way of coming up really late in the game, maybe even in production or perhaps after a massive spike in traffic.

Avoid issues caused by "launching" too many Promises at once: simply 'start' with an array of primitive values or with something like: Promise.resolve(Array.from({length: 5000}))
Then you just need to call .map and specify the concurrency limit, like so:

Promise.resolve(Array.from({length: 5000}))
  .map((x, idx) => api.activityByIndex(idx))
@justsml justsml added the task label May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant