javascript - async.auto: stop entire chain after first error -
i under impression behavior of async.auto
if 1 of tasks returned err
, global callback called error , all subsequent tasks not execute. after all, why they? global callback has been called error has been reported already.
it turns out tasks that depend on faulty task not run, , 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 code snippet, both downloadaudio
, downloadscript
run in parallel, , preprocessaudio
depends on downloadaudio
. problem when downloadscript
returns err
, preprocessaudio
still run, though there's no point because thing failed whole already:
downloading audio... downloading script... error downloading script! downloaded audio. preprocessing audio... done preprocessing audio.
i know, make preprocessaudio
depend on downloadscript
means preprocessaudio
wait downloadscript
complete, though run in parallel without problem.
is there elegant way solve this?
thanks, jan
given don't want preprocessaudio run if downloadscript has returned error, preprocessaudio 'rely' on other 2 functions.
just add dependency , 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.'); });
Comments
Post a Comment