title: How to assure you are closing your functions properly with Javascript?
body: <p>Can anyone explain to me why I am processing the 50 records but I never get <code>console.log("all records processed");</code> to the console. </p>
  
  <p>It is like I am closing a function too soon or too late. What is the best approach when working with call backs because I am sure that is why I do not get <code>"all records processed"</code>. I am using node v0.10.26 with the NPM oracle plugin. </p>
  
  <pre><code>var oracle = require('oracle');
  
  var connectData = {
      hostname: "127.0.0.1",
      port: 1521,
      database: "xe", // System ID (SID)
      user: "user",
      password: "password"
  };
  
  oracle.connect(connectData, function(err, connection) {
      if (err) {
          console.log("Error connecting to db:", err);
          return;
      }
      connection.setPrefetchRowCount(50);
      var reader = connection.reader("SELECT * FROM CARS", []);
      function doRead(cb) {
          reader.nextRow(function(err, row) {
              if (err) return cb(err);
              if (row) {
                  // do something with row
                  console.log("got " + JSON.stringify(row));
                  // recurse to read next record
                  return doRead(cb)
              } else {
                  // we are done
                  return cb();
              }
          });
      }
      doRead(function(err) {
          if (err) throw err; // or log it
          console.log("all records processed");
      });
  });
  </code></pre>