简体   繁体   中英

Async series's callback doesn't fire

My problem here is that when I use this code I always get the callback in ensureAppIsValid and the one in the Async series seems to be never fired

var ReviewProcess = function (args) {
    'use strict';
    assert(args.application, 'Need an application to review');
    this.app = args.application;
};

ReviewProcess.prototype.ensureAppIsValid = function (callback) {
    'use strict';
    if (this.app.isValid()) {
        callback(null, this.app);
    } else {
        callback(this.app.validationMessage(), null);
    }
};

ReviewProcess.prototype.processApplication = function (callback) {
    'use strict';
    async.series([
        this.ensureAppIsValid(callback)
    ], function (err, callback) {
        if (err) {
            return callback(null, {
                success: false,
                message: err
            });
        }
        callback(null, {
            success: true,
            message: 'Welcome to Mars'
        });
    });
};

Async series requires a lists of functions as tasks to be ran.

You pass

this.ensureAppIsValid(callback)

But this is the call of the function, not the function itself.

Try this:

Async.series([
        this.ensureAppIsValid.bind.apply(this.ensureAppIsValid, [null, [callback]])
    ], ... )

It looks like you're using the word 'callback' too many times and the code isn't doing what you're expecting it to. You are passing the top level callback into the ensureAppIsValid() function, so once that function executes it doesn't go to async's callback. It also looks like you don't need the extra callback in async's follow up.

How about this:

ReviewProcess.prototype.processApplication = function (callback) {
    'use strict';
    async.series([
        this.ensureAppIsValid(cb)
    ], function (err) {
        if (err) {
            return callback(null, {
                success: false,
                message: err
            });
        }
        callback(null, {
            success: true,
            message: 'Welcome to Mars'
        });
    });
};

You shouldn't pass argument callback to this.ensureAppIsValid() . Instead, use here local callback parameter. For example, named cb .

Try:

var ReviewProcess = function (args) {
    'use strict';
    assert(args.application, 'Need an application to review');
    this.app = args.application;
};

ReviewProcess.prototype.ensureAppIsValid = function (callback) {
    'use strict';
    if (this.app.isValid()) {
        callback(null, this.app);
    } else {
        callback(this.app.validationMessage(), null);
    }
};

ReviewProcess.prototype.processApplication = function (callback) {
    'use strict';
    async.series([
        this.ensureAppIsValid(cb)
    ], function (err, callback) {
        callback(null, {
            success: !err,
            message: err? err : 'Welcome to Mars'
        });
      }
    });
};

I have also slightly changed your eventual callback in async.series . Now it's more compact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM