I'm reviewing Amazon Lambda's example code for resizing images in S3 buckets. Example code (snipped for clarity):
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({Bucket: srcBucket, Key: srcKey}, next);
},
function tranform(response, next) {
gm(response.Body).size(function(err, size) {
// do resize with image magic
}
}
]);
//... more handling code
...shows they are using async waterfall. However, each of these ordered steps seems to rely on the results of the previous function. So in essence, this is a sequential operation.
What is the benefit of using async waterfall here? Is this something to do with Lambda's execution engine at Amazon, or just a sensible design decision in node?
This is basically a sensible design decision as you described it.
Instead of getting into the "callback hell" the author of the example basically flattened the code by using waterfall.
The alternative code would look like this:
s3.getObject({Bucket: srcBucket, Key: srcKey}, function(response){
gm(response.Body).size(function(err, size) {
// do resize with image magic
}
});
Which is less readable, and can get much more complex and much less readable as steps are added to the processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With