0

I'm hoping someone who uses the Q module in Node.js can advise me.

So, my question is:

Is it typically necessary to create more than one promise if you are trying to perform multiple functions in sequence?

For example, I'm trying to create an application that:

1) Read data from a file (then)
2) Opens a database connection (then)
3) Executes a query via the database connection (then)
4) Store JSON dataset to a variable (then)
5) Close Database connection. (then)
6) Perform other code base on the JSON dataset I've stored to a variable.

In raw Node.js, each method of an object expects a callback, and in order to do these tasks in the proper order (and not simultaneously -which wouldn't work), I have to chain these callbacks together using an ugly amount of code nesting.

I discovered the Q module, which prevents nesting via the Promise concept.

However, in my first attempt at using Q, I'm trying to make a promise out of everything, and I think I might be over-complicating my code.

I'm think that maybe you only really have to create one promise object to perform the steps mentioned above, and that it may be unnecessary for me to convert every method to a promise via the Q.denodeify method.

For example, in my code I will be connecting to a db2 database using the ibm_db module. Probably due to misunderstanding, I've converted all the ibm_db methods to promises like this:

var ibmdb = require('ibm_db');
var q = require('q');
var ibmdbOpen = q.denodeify(ibmdb.open);
var ibmdbConn = q.denodeify(ibmdb.conn);
var ibmdbClose = q.denodeify(ibmdb.close);
var ibmdbQuery = q.denodeify(ibmdb.query);

Is this really necessary?

In order to do one thing after another in Q, is it necessary for me to denodeify each method I'll be using in my script?

Or, can I just create one promise at the beginning of the script, and use the q.then method to perform all the asynchronous functions in sequence (without blocking).

Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
  • 1
    If you are going to use promises, then yes, it makes sense to make a promise generating function for each. You might also look into using the async library as an alternative to promises (https://github.com/caolan/async), although I personally prefer promises. – Mike Atkins Feb 28 '15 at 00:47

1 Answers1

0

Is it typically necessary to create more than one promise if you are trying to perform multiple functions in sequence?

Yes, definitively. If you didn't have promises for all the intermediate steps, you'd have to use callbacks for them - which is just what you were trying to avoid.

I'm trying to make a promise out of everything

That should work fine. Indeed, you should try to promisify on the lowest possible level - the rule is to make a promise for everything that is asynchronous. However, there is no reason to make promises for synchronous functions.

Especially your steps 4 and 5 trouble me. Storing something in a variable is hardly needed when you have a promise for it - one might even consider this an antipattern. And the close action of a database should not go in a then handler - it should go in a finally handler instead.

I'd recommend not to use a linear chain but rather:

readFile(…).then(function(fileContents) {
    return ibmdbOpen(…).then(function(conn) {
        return imbmdbQuery(conn, …).finally(function() {
             return imbdbClose(conn);
        });
    }).then(function(queriedDataset) {
        …
    });
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375