2

I am using async module with waterfall method.

async.waterfall([
        function(callback) {
            ...
            callback(err);
        },
        function(result, callback) {
            console.log("This function should not be executed");
        }
    ],
    function(err) {
        if (err) {
            next(err);
            return;
        }
    }
);

But the second function always execute. How to prevent it?

Sergiy
  • 611
  • 7
  • 16
  • 2
    It should not. Make sure that `err` is a truthy value. – Bergi Mar 03 '17 at 23:22
  • Make sure to always return when calling a callback early, otherwise you will cause multiple callbacks and unpredictable behavior in many cases - advice from here: caolan.github.io/async – gaborp Feb 27 '18 at 08:30

1 Answers1

5

Try adding a return

async.waterfall([
        function(callback) {
            ...
            return callback(err); //note return here
        },
        function(result, callback) {
            console.log("This function should not be executed");
        }
    ],
    function(err) {
        if (err) {
            next(err);
            return;
        }
    }
);
Alex
  • 37,502
  • 51
  • 204
  • 332