Jan Van den bosch February 2016

async.auto: stop entire chain after first error

I was under the impression that the behavior of async.auto was that if one of the tasks returned an err, the global callback would be called with this error and all subsequent tasks would not execute. After all, why would they? The global callback has been called so the error has been reported already.

It turns out that only tasks that depend on the faulty task will not run, and the rest will.

'use strict';

var async = require('async');

async.auto({
    downloadAudio: function (callback) {
        console.log("downloading audio...");
        setTimeout(function () {
            console.log("downloaded audio.");
            callback();
        }, 10000);
    },
    downloadScript: function (callback) {
        console.log("downloading script...");
        setTimeout(function () {
            return callback(new Error("ERROR downloading script!"));
        }, 1000);
    },
    preprocessAudio: ['downloadAudio', function (callback) {
        console.log("preprocessing audio...");
        setTimeout(function () {
            console.log("done preprocessing audio.");
            callback();
        }, 5000);
    }]
}, function (err) {
    if (err) {
        return console.error(err.message);
    }

    console.log('done.');
});

In this code snippet, both downloadAudio and downloadScript will immediately run in parallel, and preprocessAudio depends on downloadAudio. The problem is that when downloadScript returns an err, preprocessAudio will still run, even though there's no point because the thing failed as a whole already:

downloading audio...
downloading script...
ERROR downloading script!
downloaded audio.
preprocessing audio...
done preprocessing audio.

I know, I could make preprocessAudio depend on downloadScript also bu

Answers


Seonixx February 2016

Given then that you don't want preprocessaudio to run if downloadScript has returned an error, preprocessaudio really does 'rely' on the other two functions.

Just add the dependency and all should work correctly.

'use strict';

var async = require('async');

async.auto({
    downloadAudio: function (callback) {
        console.log("downloading audio...");
        setTimeout(function () {
            console.log("downloaded audio.");
            callback();
        }, 10000);
    },
    downloadScript: function (callback) {
        console.log("downloading script...");
        setTimeout(function () {
            return callback(new Error("ERROR downloading script!"));
        }, 1000);
    },
    preprocessAudio: ['downloadAudio', 'downloadScript', function (callback) {
        console.log("preprocessing audio...");
        setTimeout(function () {
            console.log("done preprocessing audio.");
            callback();
        }, 5000);
    }]
}, function (err) {
    if (err) {
        return console.error(err.message);
    }

    console.log('done.');
});

Post Status

Asked in February 2016
Viewed 3,733 times
Voted 7
Answered 1 times

Search




Leave an answer