thegecko / webusb

Node.js implementation of the WebUSB Specification
https://thegecko.github.io/webusb/
MIT License
183 stars 27 forks source link

Use async/await #54

Open dsanders11 opened 5 years ago

dsanders11 commented 5 years ago

Not going to open a PR for this because I haven't tested it beyond compilation, and not sure it's something you'll want.

Switching the code to using async/await instead of manually creating promises can simplify some of the code, reduces indentation, and shaves 90 lines off the source.

See 5902723453e7f7c215a862d838882f5904dde621 for a commit that switches most code over to this pattern.

In particular, I think devicetoUSBDevice simplifies nicely, as does openDevice. It also makes the code from #39 more robust, as Promise.all will short-circuit if one of the promises rejects, moving on to closing the adapter while the other promises are still unresolved, which might cause an error.

Some food for thought.

thegecko commented 5 years ago

Aync/await in typescript created bulky code when this library was initially written, so it was avoided. However I'm keen to switch to this method of doing things in a lot of my libraries. The problem is time and balancing potential bug introductions with new features :)