0

In my create, read, update, and delete functions, I am returning true if the transaction was successful and false otherwise - except for read, where I return the requested data or null.

This strategy should make testing easy due to simply assessing the return value given various test inputs to a test database.

My problem is that I cannot fully determine whether the transaction was successful until db.query("...") completes and its callback is invoked, but, by this time,the original CRUD function has returned.

What is the common practice?

1 Answers1

1

Javascript is Asynchronous, and you'll definitely want to understand how that works when using node.

There's a lot of information on the net about this, but here's a SO answer:

Understanding Asynchronous Code in Layman's terms

Generally, best practice in your case is to use promises. Promises are very useful but can also take some time to learn. I personally use the Q promise library

https://github.com/kriskowal/q

I truly hope this helps.

Community
  • 1
  • 1
drj
  • 533
  • 2
  • 16