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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.