I have a function that needs to query a database for items meeting a specific criteria then iterate over each item checking to see if it meets multiple further criteria and finally to take some action.
- First, get all books with a certain status
- Next, see if each book is checked out
- If it is checked out go see who has it, retrieve their email and send them a notice
- Regardless of checked out status send notices to the book manager and book owner
- Update the books status in the database
I started doing part of this in a non-async fashion and it actually worked. However, once I tried to determine if the book was checked out or not I ran into problems as that value was not retrieved before the function completed running. I tried using promises but found that they were still not getting the value back in time unless I stuck the rest of my code inside the then method but that led to needed values defined prior to the then not being available inside of it.
What is the best way to approach doing something like this? Something that needs to dynamically populate multiple values at run time from multiple database tables, but only under certain conditions and all while iterating over multiple records?