I'm a little confused to as how to properly use the async module. Say I have:
result = long_sync_io_function();
short_sync_function(result);
... //other stuff dependent on result
Generally, in Node you convert long_sync_io_function() to its asynchronous counterpart long_async_io_function(callback(err, result)) and do something like:
long_async_io_function(function(err, result) {
if (!err) {
short_sync_function(result);
...//other stuff dependent on result
}
});
But the constant embedding of callbacks quickly means lots and lots of indentation. Is the correct way to use async:
//1
async.waterfall([
function(callback) {
result = long_sync_io_function();
callback(null, result);
},
function(result, callback) {
short_sync_function(result);
//other stuff dependent on result
}
]);
or
//2
async.waterfall([
function(callback) {
long_async_io_function(callback);
},
function(result, callback) {
short_sync_function(result);
...//other stuff dependent on result
}
]);
Are these equivalent? I can't tell if aysnc helps create asynchronous code as in 1, or just aids in structuring existing asynchronous code as in 2.