Pitfalls of Promisifying by hand

When wrapping a traditional style callback naively into a Promise, you might end up with code that looks like this

1     var deferred = Q.defer();
2     fs.readFile(file, "UTF-8", function(error, text) {
3         if (error) {
4             deferred.reject(new Error(error));
5         } else {
6             deferred.resolve(text);
7         }
8     });
9     return deferred.promise;

calling this deferredRead(), you would then use it

1 deferredRead("words.txt").then((words) => {
2     // do something with the data
3 }).fail((e) => {
4     // error handling
5 });

There exists 2 very serious shortcomings in this approach. Can you spot them?

Initiating function throws exception

If the initiating function throws exception, it will not propagate to the fail()-handler. That is, before the deferred is properly set up if fs.readFile() throws an error, the program will crash due to uncaught exception.

A better way would be to wrap the initiating function inside a try-catch block.

Result callback is called multiple times

The Promises/A+ spec states that a promise can only be fulfilled or rejected once.

promise.then(onFulfilled, onRejected)

A promise must provide a then method to access its current or eventual value or reason.

If onFulfilled is a function, it must not be called more than once.
(Promises/A+ Section 2.2.2)

The wrapping function does not take this into account. It allows the callback to be called infinite number of times. A better way would be to wrap the callback function around a gatekeeper logic that allows it to be run only once and that would throw error otherwise.

On the other hand, ES6 spec states that if a promise is already resolved then further resolutions are just ignored. This would argue that in this case error should not be thrown. Implementations on this vary among promise libraries: kew does throw error but major ones like Bluebird, Q or native Promises do not.

The perfect Promises/A+ version

1     var deferred = Q.defer();
2     var resolved = false;
3     try {
4         fs.readFile(file, "UTF-8", function(error, text) {
5             if (resolved) {
6                 throw new Error("Already resolved.");
7             }
8             resolved = true;
9             if (error) {
10                 deferred.reject(new Error(error));
11             } else {
12                 deferred.resolve(text);
13             }
14         });
15     } catch (e) {
16         // needed to handle special case when callback is called
17         // synchronously, normally it is asynchronous
18         if (e.message === "Already resolved.") {
19             throw e;
20         }
21         deferred.reject(e);
22     }
23     return deferred.promise;

This is here for the sake of reference. However, you shouldn't try to write this beast every time you need a callback wrapped inside a promise. It's pretty error prone and you easily miss something. Better alternative is to use for example Bluebird.promisify() for this (see also promisifyAll() for doing this to a complete module at once).

The perfect ES6 native Promises version

1     return new Promise((resolve, reject) => {
2         fs.readFile(file, "UTF-8", function(error, text) {
3             if (error) {
4                 reject(new Error(error));
5             } else {
6                 resolve(text);
7             }
8         });
9     });

If we leverage ES6 features we can write this more elegantly. The explicit try-catch can be eliminated since the constructor will call the reject handler in case an exception occurrs in the function. We can also remove the gatekeeper logic since if resolved more than once, the further resolves are just ignored.

But again, consider using ready made library for promisifying callbacks.

Node doesn't wait for your database call to finish?

Node doesn't wait for your database call to finish?

Learn how asynchronous calls work and make your app run as you intended. Get short email course on asynchronicity and two chapters from Finish Your Node App.

Take Free Course