Hi everyone ^.^~ . I bet most of us know about Promise and its benefits. But to use it is not really easy. This is example:
BluebirdJS will make your life much easier.
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.
- 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 'fs' module.
In the next section. I will introduce more details features in BluebirdJS. See you (-^.^~)