Wait for asynchronous callback function inside synchronous one
<p>I'm using <code>Express JS</code> with <code>mysql2-node</code> module to create a function that access MySQL database</p>

<p><strong>indexOfUserInVotingList</strong> :</p>

<pre><code>indexOfUserInVotingList: function (voteType, articleID, userID) {
        SQLconnection.connectToServer();
        db = SQLconnection.getConnectionInstance();
        let userIndex;
        switch (voteType) {
            case "upvote":
                db.query('SELECT upVoters FROM article WHERE article.idArticle = ?', [articleID], function (err, rows) {
                    if (err) {
                        throw err;
                    } else {
                        let upvoterArray = rows[0].upVoters;
                        userIndex = upvoterArray.indexOf(userID);
                    }
                });
                break;
            case "downvote":
                db.query('SELECT downVoters FROM article WHERE article.idArticle = ?', [articleID], function (err, rows) {
                    if (err) {
                        throw err;
                    } else {
                        let downvoterArray = rows[0].downVoters;
                        userIndex = downvoterArray.indexOf(userID);
                    }
                });
                break;
            default:
                break;
        }
        return userIndex;
    }
</code></pre>

<p>and I call this function inside this one - <strong>upvoteInArticleFromUser</strong>, which need the index of that userID to work:</p>

<pre><code>upvoteInArticleFromUser: function (articleID, userID, callback) {
        SQLconnection.connectToServer();
        db = SQLconnection.getConnectionInstance();
        let userIndex = this.indexOfUserInVotingList('upvote',articleID,userID);
        console.log("userIndex: "+userIndex);
        // the rest of the code was cut shorted...
    }
</code></pre>

<p>And I get the result:</p>

<blockquote>
  <p>userIndex: undefined</p>
</blockquote>

<p>I learnt that <code>return</code> action in <strong>indexOfUserInVotingList</strong> executed immediately before <code>mysql-query</code> run and update the value.</p>

<p>Is there anyway I can force the <strong>indexOfUserInVotingList</strong> to wait for the query to finish and return the result ?</p>

<p>The most important point is, I don't want to turn that into async function (although this approach would work):</p>

<pre><code>indexOfUserInVotingList: function (voteType, articleID, userID, callback) {
     //......... 
     //after query from database
     return callback(null,userIndex);
    }
</code></pre>

<p>..because I don't want to stuck inside <strong>callbackhell</strong>, like:</p>

<pre><code>upvoteInArticleFromUser: function (articleID, userID, callback) {
       //.....


       indexOfUserInVotingList('upvote',articleID,userID,function(err,index){
            if(err) throw err;
            else
            {
                this.userIndex = index;
                // the whole remaining code for processing would be nested inside this one...
            }
 }
</code></pre>