Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of async in Amazon Lambda example?

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?

like image 730
Allyl Isocyanate Avatar asked May 03 '26 19:05

Allyl Isocyanate


1 Answers

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.

like image 149
Ita Avatar answered May 05 '26 09:05

Ita