Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

async/await pattern in audit #27

Open
dylankb opened this issue Mar 30, 2018 · 0 comments
Open

async/await pattern in audit #27

dylankb opened this issue Mar 30, 2018 · 0 comments

Comments

@dylankb
Copy link
Member

dylankb commented Mar 30, 2018

We were hoping to use the async/await pattern in audit to solve a problem where we need to asynchronously return data from the auditFile method in order to pass the correct data object into the data event handler for the CLI to access. However, after a bit of research I’m a little skeptical of getting async/await type things to work quickly* the way we were thinking about. The problem is that auditFile executes a series of synchronous actions, then, those synchronous actions trigger events that have handlers. These events (mostly the ‘data’ event) and their event handlers then execute asynchronously. You’d have to do something differently to have all methods called in the audit operation execute asynchronously and be tied to their event handler.

The basic issue is that async works with functions that returns promises. Therefore I think what you would have to do to make this strategy work is make all audit related methods promise based, but I’m not sure I’d be able to pull that off soon. The way you’d start off is to have the entire auditShardData method body inside the return a promise (i.e return Promise.new(resolve, reject …) where it resolves in the data event handler. Article 2. shows a decent example of this. The problem then is we have 3/4 other methods in between there and auditFile method we need to return data from, which would also have to be promisified.

Options to explore are:

  • promisfying audit related method
  • promise based net.Socket wrapper libraries
  • custom events using event emmitter

Some related links:

  1. https://github.com/mkloubert/node-simple-socket - socket library that’s promise based. not popular at all, though
  2. https://techbrij.com/node-js-tcp-server-client-promisify - example of promisfying a client send method
  3. https://www.ibm.com/developerworks/community/blogs/binhn/entry/creating_a_tls_tunnel_with_node_js_and_promise?lang=en - just another example of net.connect w/ promises
  4. https://stackoverflow.com/questions/40352682/promisify-event-handlers-and-tiemout-in-nodejs - simple example of event based promise
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant