ml5js / ml5-next-gen

Repo for next generation of ml5.js: friendly machine learning for the web! 🤖
https://ml5js.org/
Other
84 stars 23 forks source link

Question: choice behind paring down of the `callback(error, results)` syntax from v0 to `callback(results)`? #198

Closed jchwenger closed 2 months ago

jchwenger commented 2 months ago

Hi lovely ml5 people!

Very glad to see the new release! I'm in the process of updating a series of examples and a question springs to mind (not a bug, really): previously, many examples using callbacks to handle predictions would follow the following syntax:

 classifier.classify(img, gotResult);

// callback
function gotResult(error, results) {
  // Display error in the console
  if (error) {
    console.error(error);
  }
  // Do something with results
}

This has now been reduced to only gotResult(results), I wondered why, and what would be your recommendation for error handling in the new framework (perhaps you included that within the model methods directly)?

I'm unsure, to be honest, if that's an ml5 question proper, or more like a JS pattern one, so apologies if this question isn't in the right place!

ziyuan-linn commented 2 months ago

Hi @jchwenger, thank you for bringing this up!

We have decided to move from gotResults(error, results) to gotResults(result, error) in the new version. We believe this interface is a little more intuitive for beginners since they can choose to ignore the error parameter.

That being said, error handling was never consistently implemented in both the v0 and v1 versions of the library. The error parameter will always be undefined on a lot of the callback functions. We should definitely look into it hooking up the error parameter soon.

For now, you could try:

function gotResult(results, error) {
  if (error) {
    // handle error
  }
  // Do something with results
}

This should be compatible with the current version (v1.0.2) and will start to fully work once we update the error handling in the library.

Let us know if you have any thoughts or suggestions!

jchwenger commented 2 months ago

Hi @ziyuan-linn,

Thanks for the quick reply, that makes complete sense! I also fully approve the choice, I also think it's more intuitive this way 🙌 !

I have been using ml5 for projects and teaching and I am eager to see the new version evolve. Is it better here, for question, or on the discord? Thanks!

ziyuan-linn commented 2 months ago

Here or discord works! Feel free to use the one you prefer.