cmseaton42 / node-ethernet-ip

A Lightweight Ethernet/IP API written to interface with Rockwell ControlLogix/CompactLogix Controllers.
MIT License
265 stars 106 forks source link

Read/Write Tags/Groups error friendly await #51

Open pcnate opened 5 years ago

pcnate commented 5 years ago

Read/Write Tags/Groups should be more await friendly by allowing const [ error, data ] = PLC.readTag( tag )

Current Behavior

You can currently await any of these functions but if an error happens, it throws an uncaught promise error which are pretty annoying to track down. The workaround is to write a wrapper for each function that returns a promise and catch errors yourself which makes the current level of support for await pointless.

Expected Behavior

You should be able to handle all errors like so:

const [ error, data ] = PLC.readTag( tag )
if ( error ) {
    // handle the error, log error, break you current block, etc
}

Another option could be to pass an error callback but... or an options object to control return structure

Possible Solution (Optional)

Each of these functions should contain a then and a catch and both should return something and not cause unhandled promise rejection errors. Returning [ false, data ] could be a breaking change however so a callback could be added to be executed on the catch block.

I currently use await-to-js to "awaitify" these functions.

Context

Trying to use async await much more than I used to and encountering unexpected issues or forgetting to use helper functions.

Steps to Reproduce (for bugs only)

  1. Use any await example from the README
  2. Try it on a tag that doesn't exist or throws some other error
  3. Program dies

Your Environment

pcnate commented 5 years ago

this might be worth implementing in v2.0