1

I want to write in Node.JS the equivalent of the following synchronous code:

function(param)
{
    if(condition1(param))     {f(param);}
    else if(condition2(param)){g(param);}
    else if(condition3(param)){h(param);}
    ...
}

Where the verification of the conditions is possibly doing some database, filesystem or even http I/O; so should not be run in such a synchronous code.

Simple but dirty solution: one can chain callbacks as in the answer to that question How to write a non-blocking if statement in Node JS?

But when the number of conditions are high it becomes easily unreadable and hard to modify (eg. to change the order of the conditions).

Is there a nice pattern that can be used to keep such a code neat?

3 Answers 3

2

The async library has a function that can help with this, series. Here is an equivalent to your code using series:

async.series([
    function(callback) {
        if (condition1(param)) {
            f(param);
        } else { callback(null); }
    },
    function(callback) {
        if (condition2(param)) {
            g(param);
        } else { callback(null); }
    },
    function(callback) {
        if (condition3(param)) {
            h(param);
        } else { callback(null); }
    }
]);

(This answer just adds more detail to this answer mentioning async and your comment mentioning series.)

Sign up to request clarification or add additional context in comments.

Comments

1

You may also want to check out the async library.

1 Comment

Thanks, using the function async.series([f,g,h]) (and putting the condition inside the functions) is the neat syntax I was looking for. (new account I can't upvote)
0

Personally, I have used Streamline with great success in my (admittedly small) node.js projects. As you can see from their page, it allows you to transform code like

function archiveOrders(date, cb) {
  db.connect(function(err, conn) {
    if (err) return cb(err);
    conn.query("select * from orders where date < ?", [date], function(err, orders) {
      if (err) return cb(err);
      helper.each(orders, function(order, next) {
        conn.execute("insert into archivedOrders ...", [order.id, ...], function(err) {
          if (err) return cb(err);
          conn.execute("delete from orders where id=?", [order.id], function(err) {
            if (err) return cb(err);
            next();
          });
        });
      }, function() {
        console.log("orders have been archived");
        cb();
      });
    });
  });
}

into

function archiveOrders(date, _) {
  var conn = db.connect(_);
  conn.query("select * from orders where date < ?", [date], _).forEach_(_, function(_, order) {
    conn.execute("insert into archivedOrders ...", [order.id, ...], _);
    conn.execute("delete from orders where id=?", [order.id], _);
  });
  console.log("orders have been archived");
}

1 Comment

Thanks, you may want to check the other answer about the async lib. It was simpler for me to understand what is going on. Would upvote though if I could...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.