3

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.