Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://github.com/caolan/async" rel="nofollow">async</a> is a popular asynchronous flow control library often used with node.js. I've never personally used it in the browser, but apparently it works there as well.</p> <p>This example would (theoretically) run your two functions, returning an object of all the filenames and their load status. <code>async.map</code> runs in parallel, while <code>waterfall</code> is a series, passing the results of each step on to the next.</p> <p>I am assuming here that your two async functions accept callbacks. If they do not, I'd require more info as to how they're intended to be used (do they fire off events on completion? etc).</p> <pre><code>async.waterfall([ function (done) { fetchFiles(source, function(list) { if (!list) done('failed to fetch file list'); else done(null, list); }); // alternatively you could simply fetchFiles(source, done) here, and handle // the null result in the next function. }, function (file_list, done) { var loadHandler = function (memo, file, cb) { loadFile(file, function(data) { if (!data) { display('failed to load: ' + file); } else { display(data); } // if any of the callbacks to `map` returned an error, it would halt // execution and pass that error to the final callback. So we don't pass // an error here, but rather a tuple of the file and load result. cb(null, [file, !!data]); }); }; async.map(file_list, loadHandler, done); } ], function(err, result) { if (err) return display(err); // All files loaded! (or failed to load) // result would be an array of tuples like [[file, bool file loaded?], ...] }); </code></pre> <p><code>waterfall</code> accepts an array of functions and executes them in order, passing the result of each along as the arguments to the next, along with a callback function as the last argument, which you call with either an error, or the resulting data from the function.</p> <p>You could of course add any number of different async callbacks between or around those two, without having to change the structure of the code at all. <code>waterfall</code> is actually only 1 of 10 different flow control structures, so you have a lot of options (although I almost invariably end up using <code>auto</code>, which allows you to mix parallel and series execution in the same function via a Makefile like requirements syntax).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload