0
app.get("/", function(req, res) {
          if( some_func() == 0 ) {
                res.send("some_func() returned zero!");
                /* leave function(req, res) */
          } 

          res.send("Everything OK.")

});

How would that work in Node.js?

Ry-
  • 218,210
  • 55
  • 464
  • 476

1 Answers1

3

You're looking for the return statement.

app.get("/", function(req, res) {
      if( some_func() == 0 ) {
            res.send("some_func() returned zero!");
            return;
      } 

      res.send("Everything OK.")
});

Or just use an else:

app.get("/", function(req, res) {
      if( some_func() == 0 ) {
            res.send("some_func() returned zero!");
      } else {
          res.send("Everything OK.")
      }
});

N.B. Perl's last is not like return; it is analogous to break in C-like languages (C, C#, Java, JavaScript, etc.), and is used to exiting loops and switches – not functions.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710