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

Technology

Using Promise easier with BluebirdJS. Why not? P1

Hi everyone ^.^~ . I bet most of us know about Promise and its benefits. But to use it is not really easy. This is an example:

Using Promise to create readFileAsync promise.

BluebirdJS will make your life much easier.

Using Promise from Bluebird to create readFileAsync promise.

It so cool. So What is BluebirdJS?

BluebirdJS is a fully-featured Promise library for JS. It allows you to turn callbacks => Promise (promisify). So we can “pomisify” other Node modules in order to use them asynchronously.

Why BluebirdJS?

  • Using promise easier: It can call the function like it’s a simple synchronous function.
  • Consolidate multiple function/promise: I’ll talk more clearly in next section.
  • Bluebird promise can be cancelled: Cancelling is often necessary to avoid throwing errors when the user navigates away while the call is in progress.
  • Easy to debug: If you forgotten to declare a .catch. Don’t worry. With Bluebird, unhandled errors are not silently swallowed by default, but reported along with helpful stack traces.
  • Runs everywhere Bluebird runs on pretty much every platform.

How to use BluebirdJS?

  • Step 1: Installing the NPM Modules, run the below command.

npm install bluebird

  • Step 2: The next step is to include the bluebird module in your code.

var Promise = require("bluebird");

  • Step 3: Now we will start with a simple features in Bluebird. It’s promisify. Using to return  a new function that return a promise instead of using a callback.

In this case, I used promisify to create readFileAsync promise from the ‘fs’ module.

In the next section. I will introduce more detailed features in BluebirdJS. See you (-^.^~)