Migrating Bluebird promises to native ones
06 February 2019
1 min read
We have used bluebird because it was one of the best libraries for promises. But then promises have gained support in both nodejs and browser. That was the easy catch so we decided to remove it from our code base.
The functions we were using of that library were
Promisify
Node.js ships with util
api which provides same requirement as that of bluebird.
The method works by taking in the common error-first callback-style functions and returns a promises.
1// Before
2
3import Promise from "bluebird";
4const readFile = Promise.promisify(fs.readFile);
5
6// After
7
8import util from "util";
9const readFile = util.promisify(fs.readFile);
Delay
Purpose of this method is to returns a promise that will be resolved after given milliseconds. Native way to do this was to promisify setTimeout function.
1// Before
2
3import Promise from "bluebird";
4await Promise.delay(1000);
5
6// After
7
8import util from "util";
9const setTimeoutAsync = util.promisify(setTimeout);
10await setTimeoutAsync(null, 1000);