vectrixdevelops / audio-mpg123

A native node binding to write raw PCM data to an OS driver.
MIT License
5 stars 0 forks source link

More elaborate errors in callbacks #11

Open dy opened 7 years ago

dy commented 7 years ago

Is there any way to remake mpg123.create((success) => {}) to mpg123.create((err) => {}), with error containing the reason why not able to create?

jamen commented 7 years ago

I do not like the (success) => ... setup either. Funny thing is, none of the functions beside mpg123.write is actually asynchronous. This was just how speaker was setup, so it went in a similar direction here.

My solution in #10 is to return booleans instead of using sync callbacks for every response. So for example:

// Edited
var speaker = mpg123.create()
if (!speaker) {
  throw new Error('Could not open speaker')
}

Throwing errors directly could also be possible!

Personally I like the boolean returns more because it is easier to handle JavaScript from JavaScript. But I get the merits of the other side and I would be open to going that direction too. Let me know what you think. :smile:

Edit: I forgot, in the case of mpg123.create, it would return a pointer or false, not only booleans. The same code is possible though.

dy commented 7 years ago

@jamen :+1: for the boolean return, that is common pattern for set.delete, map.delete and others. :+1: for sync API