6
\$\begingroup\$

I am using the following frameworks/libraries:

  • Node.js
  • Express.js
  • node-mysql.js
  • async.js

I am making multiple asynchronous queries to my DB, but each subsequent query relies on the results of the previous query, so I decided to use the waterfall method.

Right now, my code looks like this (I simplified it with only 2 queries, but there are more):

async.waterfall([
    function(cb){
        db.query(insertQuery,values,cb);
    },
    function(results,_,cb){
       var lastInsertId = results.insertId;
       db.query(anotherInsertQuery,otherValues.concat(lastInsertId),cb);
    }
],callback);

But I found my code a bit messy, especially the function(cb) { ... } wraps.

Is there a way to get rid of those annoying function(cb){...} ?

\$\endgroup\$
8
  • \$\begingroup\$ @Jamal : Thanks for the edit. I wanted to tag my post with node-mysql.js, since it's related with that, but I couldn't create it (not enough reputation). Do you think you could do that ? \$\endgroup\$ Commented Aug 20, 2014 at 14:34
  • 3
    \$\begingroup\$ At the moment I don't think we need a node-mysql.js tag here \$\endgroup\$ Commented Aug 20, 2014 at 14:36
  • \$\begingroup\$ Welcome to Code Review btw, I don't find your question very clear. Are you asking if your implementation is good, or asking for alternative implementations? \$\endgroup\$ Commented Aug 20, 2014 at 14:36
  • \$\begingroup\$ @SimonAndréForsberg : Ok I understand. I first hesitated, because I thought it was too specific, but then found out that async.js existed, with only one question so... But anyway, thanks for the welcome ! Sorry about that, I would like to know what's the cleanest way to do what I'm trying to do using the async.js module. What methods (apply, waterfall...) or structure should I use ? Is that more clear ? Do you think I should edit my question ? \$\endgroup\$ Commented Aug 20, 2014 at 14:41
  • \$\begingroup\$ What exactly is the question @WaldoJeffers? Short and simple, because this is a pretty unclear question. \$\endgroup\$ Commented Aug 20, 2014 at 14:55

2 Answers 2

3
\$\begingroup\$

I think not.

Whereas you find the function(cb) { ... } wraps a bit messy, I think that this is the most elegant way to show readers that this is a separate functions, and that stuff is about to get asynchronous.. Again, compared to other approaches, this is quite clean.

Also, (I know this is just a small example), consider not using the lastInsertId variable. You could simply go for

async.waterfall([
    function(cb){
        db.query(insertQuery,values,cb);
    },
    function(results,_,cb){
       db.query(anotherInsertQuery,otherValues.concat(results.insertId),cb);
    }
],callback);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks @konijn. I'll wait a bit to see if someone comes with another answer, and will accept yours otherwise. \$\endgroup\$ Commented Aug 25, 2014 at 8:43
2
\$\begingroup\$

One ES-6 technique is to use the await keyword. As the MDN documentation says:

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨March 2017⁩. 1

Obviously that was was a few years after the code was posted, however it has been supported since NodeJS version 7.62 and hopefully the NodeJS version the code is running on is much greater given LTS. The documentation also states:

The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async function or at the top level of a module. 1

I haven't used node-mysql.js, and am having difficulty finding the documentation and/or source of it (perhaps because it may not have been maintained in the past 11 years), however depending on its support for asynchronous queries, one could use a try/catch block (as the MDN documentation for await suggests), or possibly "handle rejected promises without a try block by chaining a catch() handler before awaiting the promise."3 to call the callback function.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.