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}`)
})
Top comments (0)