Comment

Comment by athenot

This is where syntactic sugar can be helpful. CoffeeScript can make the example a lot more readable by removing the unnecessary braketing when the last argument of a function is a callback (common case in node.js). The only thing that's still ugly is the .bind() applied to a function, which somewhat messes with the style. Other than that, this makes callbacks flow a lot more naturally when reading source code.

(Of course, as it is for all stylistic considerations, it's a matter of taste, not absolutes.)

    fs.readdir source, (err, files) ->
      if err
        console.log 'Error finding files: ' + err
      else
        files.forEach (filename, fileIndex) ->
          console.log filename
          gm(source + filename).size (err, values) ->
            if err
              console.log 'Error identifying file size: ' + err
            else
              console.log filename + ' : ' + values
              aspect = values.width / values.height
              widths
                .forEach ((width, widthIndex) ->
                  height = Math.round(width / aspect)
                  console.log "resizing #{filename} to #{height}x#{height}"
                  this.resize width, height
                    .write "#{dest}w#{width}_#{filename}", (err) ->
                    if err then console.log 'Error writing file: ' + err
                ).bind this
parent athenot 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton