title: Applying callback design pattern with Node-JS
body: <p>I believe I understand the design pattern of a callback in a basic form. I however don't know if and how to apply this design pattern to this more complex situation.</p>
  
  <p>I'm writing a set of JavaScript functions to be used server-side. These functions have to return data which is fetched from a local database if possible, from an external API otherwise. What I want to do is, in summary:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//dataProvider.js
  function DataProvider (dbCredentials, apiCredentials) {
    DB = require('./dbStuff');
    API = require('./apiStuff');
    this.db = new DB(dbCredentials);
    this.api = new API(apiCredentials);
    
    this.getSpecificData = getSpecificDataWrapper(this.db, this.api);
  }
  
  function getSpecificDataWrapper(db, api) {
    return function (extraQueryInfo) {
      var result = db.genericGetFunc(specificDBQuery);
      if (!satisfyingResult(result)) {
        result = api.genericGetFunc(specificAPIQuery);
      return result;
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>When calling this function from the main <code>app.js</code> file, I would like to do this as <code>dataProviderInstance.getSpecificData(extraQueryInfo)</code>. Right now I have a similar structure set up for the database and api functions which execute the queries. But as these queries take time, the <code>getSpecificData</code> always returns undefined.</p>
  
  <p>As code is in different files, the usage of so many call backs would make me go insane. So how do I use callbacks with these two or more level deep function calls?</p>