DEV Community

Marcos Henrique
Marcos Henrique

Posted on

4 2

Batch processing flavor with Bluebird.map() πŸ”­πŸŒŒ

Slight introduction β›Ί

Bluebird is a fully-featured Promise library for JavaScript.

The strongest feature of Bluebird is that it allows you to "promisify" other Node modules in order to use them asynchronously.

Promisify is a concept applied to callback functions.

This concept is used to ensure that every callback function which is called returns some value.

Bluebird.map it's awesome for batch handling πŸ“¦

Let's take as a premise a finite iterable like an array, or a promise of an iterable, which produces promises (or a mix of promises and values), iterate over all the values in the Iterable into an array and map the array to another using the given mapper function.

Promises returned by the handler function are awaited for and the returned promise doesn't fulfill until all mapped promises have fulfilled as well.

If any promise in the array is rejected, or any promise returned by the mapper function is rejected, the returned promise is rejected as well.

The handler function for a given item is called as soon as possible, that is, when the promise for that item's index in the input array is fulfilled.

This doesn't mean that the result array has items in random order, it means that .map can be used for concurrency coordination unlike .all.

The concurrency limit applies to Promises returned by the handler function and it basically limits the number of Promises created.

Real world example πŸ™

const updateLotCollection = message => (
    lotList = []
  ) => 
    Bluebird.map(lotList,
      lotItem => someAsyncHandler(lotItem),
      { concurrency: +env.concurrencyQuantity }
    )
      .then(data => BatchModel.insertMany(data))
      .tap(batchDocument => {
        Logger.info(`Batch Document created {} - ${batchDocument}`)
      })
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❀️