Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you show what your first example would look like "fixed"? #31

Open
cmoser-edge opened this issue Feb 8, 2019 · 2 comments
Open

Comments

@cmoser-edge
Copy link

You showed an example (reading sizes of files) that seems very difficult to make better and even after following your steps, it is tough to read and understand. I would love to see the right way to do that.

@adrian-skybaker
Copy link

adrian-skybaker commented Apr 9, 2021

I think it's because the most important way to avoid callback hell is not suggested: don't use asynchronous calls everywhere.

@lynellf
Copy link

lynellf commented May 13, 2022

You showed an example (reading sizes of files) that seems very difficult to make better and even after following your steps, it is tough to read and understand. I would love to see the right way to do that.

Here's how I'd write it.

function onError(err) {
  if (err) {
    console.log('Error writing file: ' + err)
  }
}

function handleWidths(filename, aspect, resize) {
  return function (width, widthIndex) {
    var height = Math.round(width / aspect)
    console.log('resizing ' + filename + 'to ' + height + 'x' + height)
    resize(width, height).write(dest + 'w' + width + '_' + filename, onError)
  }
}

function handleSize(filename) {
  var resize = this.resize
  return function (error, values) {
    if (error) {
      return console.log('Error identifying file size: ' + err)
    }

    console.log(filename + ' : ' + values)
    var aspect = (values.width / values.height)
    widths.forEach(handleWidths(filename, aspect, resize))
  }
}

function handleFile(gm) {
  return function (filename, fileIndex) {
    console.log(filename)
    gm(source + filename).size(handleSize(filename))
  }
}

function onRead(gm) {
  return function (err, files) {
    if (error) {
      return console.log("Error finding files: " + error")
    }

    files.forEach(handleFile(gm))
  }
}

fs.readdir(source, onRead(gm))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants