TECH TOWN 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 (-^.^~)


TECH TOWN –  TECHNOLOGY CONSULTING FOR YOUR BUSINESS

CONTACT US:

📞Phone: +84 81-716-0331

🌐Website: https://techtown.asia

📧Email: info@techtown.asia

📍Address: Floor 6, Vinhomes Central Park, No 208 Nguyen Huu Canh, Ward 22, Binh Thanh District , HCMC.

– Japan Representative : Floor 7, Honmachi Minami Garden City, 3-6-1 Kitakyuhoji Machi, Chuo-Ku, Osaka, 541-0057, Japan

– United States Representative office: 10802 Capital Ave, Garden Grove, CA 92843, United States

– Canada Representative office: 100 City Centre Dr. Unit 206 – Level 1, Mississauga, ON, L5B 2C9

Author

admin