Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>TL;DR:</strong> Use <code>readdirSync()</code> instead of <code>readdir()</code> if you're just planning to read local files at startup time. If you're planning to actually read data from remote database or do any I/O at runtime, use your option #2 - the callback. Explanation and code examples below.</p> <p><strong>Detailed explanation:</strong></p> <p>While at first this might seem like a module/dependecy/require-related question, it's really not. It's a generic question of how to handle <strong>asynchronous code</strong>. Let me explain:</p> <p><code>require()</code> is basically the only <strong>synchronous</strong> function widely used throughout node that deals with I/O (it requires other modules from filesystem). Synchronous means it actually returns it's data as return value, instead of calling a callback.</p> <p>The most basic 101 rule in asynchronous programming is:</p> <blockquote> <p>You can <strong>never</strong> take an asynchronous piece of code and create a synchronous API for it.</p> </blockquote> <p><code>require</code> uses a special <strong>synchronous</strong> version of <code>readFile</code> called <code>readFileSync</code>. Since modules are really only loaded at the start of the program, the fact that it blocks the node.js execution while it's reading the module is not a problem.</p> <p>In your example however, you try to perform additional <strong>asynchronous I/O</strong> - <code>readdir()</code> done during the require stage. Thus, you either need to use <strong>synchronous</strong> version of this command or the API needs to change...</p> <p>So there's the background to your problem.</p> <p>You identified the two basic options:</p> <ol> <li>using a <em>promise</em> (which is essentially the same as your <code>EventEmitter</code> example)</li> <li>using a <em>callback</em> (your second example shows this well) and a third is:</li> <li>using a <strong>synchronous</strong> version of the <code>readdir()</code> command called <code>readdirSync()</code></li> </ol> <p>I would use the <strong>option #3</strong> for simplicity reason - but only if you're planning to just read a couple files at startup time as your example implies. If later your DB module is actually going to connect to a database - or if you're planning to do any of this at runtime, jump the boat now and go with async API.</p> <p>Not many people remember this anymore, but promises were actually the original default of how to handle async in node.js. In node 0.1.30 however promisses were <a href="https://github.com/joyent/node/commit/bb0d1e">removed and replaced</a> by a standardized callback with the <code>function(err, result)</code> signature. This was done largely for simplicity reasons.</p> <p>These days, vast majority of your async calls takes this standard callback as the last parameter. Your database driver does it, your web framework does it - it's everywhere. You should stay with the prevalent design and use it too.</p> <blockquote> <p>The only reason to prefer promises or events is if you have <strong>multiple different results</strong> that can happen. For example a socket can be opened, receive data, be closed, flushed etc.</p> </blockquote> <p>This is not your case. Your module always does the same (reads some files). So <strong>option #2</strong> it is (unless you can stay <strong>synchronous</strong>).</p> <p>Finally, here are the two winning options rewritten slightly:</p> <p><strong>Synchronous option:</strong><br> <em>good just for local filesystem at startup time</em></p> <pre><code>// db.js var fs = require('fs'); exports = fs.readdirSync('.'); // main.js var db = require('./db'); // insert rest of your main.js code here </code></pre> <p><strong>Asynchronous option:</strong><br> <em>for when you want to use DBs etc.</em></p> <pre><code>// db.js var fs = require('fs'), cached_files; exports.init = function(callback) { if (cached_files) { callback(null, cached_files); } else { fs.readdir('.', function(err, files) { if (!err) { cached_files = files; } callback(err, files); }); } }; // main.js require('./db').init(function(err, files) { // insert rest of your main.js code here }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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