Over 10 years we help companies reach their financial and branding goals. Engitech is a values-driven technology agency dedicated.

Gallery

Contacts

L17-11, Floor 17, Vincom Center Building, No 72 Le Thanh Ton, Ben Nghe Ward, District 1, HCMC

info@waostudio.com

+84 81-716-0331

Development Technology

Using Promise easier with BluebirdJS. Why not? P2

Hi everyone ^. ^~. In the previous section, I introduced about BluebirdJS and how to use it on here . Today, I’ll talk about more details features.

Now, Let Started! In the part 1, we talked about promisify function. It converts a single callback taking function into a promise returning function. And we could also even more conveniently. if you have any library and it has callback functions. And you want to also add promises to them. Don’t worry, BluebirdJs has promisifyAll function.  

  • With PromisifyAllPromise.promisifyAll(object, {options}) – takes an object full of functions and add a “Promise” version for it.  

If we have a lot of promises and we want to use them. In this case we will have Promise.all. Takes an array of promises which it starts executing at the same time.

  • With Promise.allPromise.all([promises]) – return an array of the results.
  • And more useful with .spread() – flatten returning array.
  • Besides, we also have Promise.join(). It is very similar to Promise.all() but take the promises themselves, not an array. And more performance.

Go to the next features. This is a function that i think the most useful function of BluebirdJS. It allows you to use .map() so map will cut a lot of the work you have. This is an example:

  • Promise.map(array, function(element){assign promise to each element}) – return array of results.
  • Besides Promise.map()  also have optional last parameter:{concurrency: }. Using to limit request made at a time.
In this case, This will now only make 2 requests at a time.

BluebirdJS has a few more methods that happen non concurrently or sequentially.

  • Promise.mapSeries() – Promise.map() with concurrency: 1.
  • Promise.each() – side effects of each item in an array.
  • Promise.reduce() – stop making request when reduce exceeds a threshold.

You can read more details on the home page of BluebirdJS on here. Good bye! See you later.