Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: This is a more in depth example based on use of the connection library you seem to be using. Please note, some of this is javascript psuedo code. Things like adding objects to the resultsArray are clearly not complete, the only thing I took time to make sure was correct is the "flow of logic" as it pertains to callbacks. Everything else is for you to implement. In order to support multiple calls to the same callback function and maintain state from call to call, the best way is to wrap the set of callbacks in a closure. This allows the callbacks to share some state with the main event loop. This allows you to pass arguments to the callbacks, without actually having to pass them as arguments, much like class variables in c++, or even globals in javascript, but we haven't poluted the global scope :)</p> <pre><code>function queryDataBase(query) { //wrap the whole query in a function so the callbacks can share some //variables with similar scope. This is called a closure int rowCounter = 0; var dataRowsFromStep2; var resultsArray = {}; connection.query(query, dataBaseQueryStep2); function dataBaseQueryStep2(err, rows) { //do something with err and rows dataRowsFromStep2 = rows; var query = getQueryFromRow(dataRowsFromStep2[rowCounter++]);//Always zero the first time. Might need to double check rows isn't empty! connection.query(query, dataBaseQueryStep3); } function dataBaseQueryStep3(err, rows) { //do something with err and rows if(rowCounter &lt; dataRowsFromStep2.size) { resultsArray.add(rows);//Probably needs to be more interesting, but you get the idea //since this is within the same closure, rowCounter maintains it's state var query = getQueryFromRow(dataRowsFromStep2[rowCounter++]); //recursive call query using dataBaseQueryStep3 as it's callback repeatedly until //we run out of rows to call it on. connection.query(query, dataBaseQueryStep3) } else { //when the if statement fails we have no more rows to run queries on so return to main program flow returnToMainProgramLogic(resultsArray); } } } function returnToMainProgramLogic(results) { //continue running your program here } </code></pre> <p>I personally like the above logic better than the syntax async produces... I believe the heart of your problem rests in your nested calls to async, and the fact that ASYN itself, runs the series of functions asynchronously, but in order(confusing I know). If you write your program like this, you won't have to worry about it!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload