apache / openwhisk-package-kafka

Apache OpenWhisk package for communicating with Kafka or Message Hub
https://openwhisk.apache.org/
Apache License 2.0
33 stars 43 forks source link

Improve Promise usage by using chaining #276

Open markusthoemmes opened 6 years ago

markusthoemmes commented 6 years ago

Promises in the actions are plumbed together in a non-consistent way. Sometimes they use then/catch chaining, sometimes they are constructed globally and then resolve/reject is used. Sometimes, both ways are even mixed together.

We should review the code and fix all usages of Promises to use chaining. That's more readable in general and gives more guarantees on the Promise being actually resolved at some point.

jberstler commented 6 years ago

After doing a quick review, the only place I found where this can likely be tidied up is the main Promise in the web actions, for example: https://github.com/apache/incubator-openwhisk-package-kafka/blob/457f76dbd930a573fcfb427890a6c84645c8b61a/action/kafkaFeedWeb.js#L17

It seems that all paths within this function already return a Promise, and so the manually-constructed Promise is not needed.

However, all other instances of manually constructed Promises I found appear to be necessary as they are needed:

Of these, the only one I can think could be changed would be the last one, and that would require using nano in a way that natively returns Promises - not sure if that is possible, but it is worth investigating.

markusthoemmes commented 6 years ago

@jberstler thanks for having a look!

To the first needed example: Couldn't every return path return an explicit Promise.resolved/Promise.reject instead of wrapping it? Dunno if that helps readability though.

The second is indeed how I expected to find things.

The third one could use https://nodejs.org/api/util.html#util_util_promisify_original (if that's available in the node version used).

The wrapping promise particularly nasty though, I think that should vanish as you pointed out.