Skip to content

Commit b2db2c2

Browse files
committed
Added async require
1 parent a584173 commit b2db2c2

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎chapters/modules.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Lets create a utility module named _utilities_, which will contain a `merge()` f
1010
var utilities = {};
1111
utilities.merge = function(obj, other) {};
1212

13-
CommonJS modules remove this conflict by "wrapping" the contents of a JavaScript file with a closure similar to what is shown below, however more pseudo globals are available to the module, not just `exports` and `module`. The `exports` object is then returned when a user invokes `require('utils')`.
13+
CommonJS modules remove this conflict by "wrapping" the contents of a JavaScript file with a closure similar to what is shown below, however more pseudo globals are available to the module, not just `exports`, `require`, and `module`. The `exports` object is then returned when a user invokes `require('utils')`.
1414

1515
var module = { exports: {}};
1616
(function(module, exports){
@@ -53,7 +53,6 @@ Node also supports the idea of _index_ JavaScript files, to illustrate this exam
5353

5454
The contents of _./math/add.js_ show us a new technique, here we use `module.exports` instead of `exports`. Previously mentioned was the fact that `exports` is not the only object exposed to the module file when evaluated, we also have access to `__dirname`, `__filename`, and `module` which represents the current module. Here we simply define the module export object to a new object, which happens to be a function.
5555

56-
5756
module.exports = function add(a, b){
5857
return a + b;
5958
};
@@ -72,4 +71,14 @@ if we change our module slightly, we can remove `.Animal`:
7271

7372
which can now be used without the property:
7473

75-
var Animal = require('./animal');
74+
var Animal = require('./animal');
75+
76+
## Asynchronous Require
77+
78+
Node provides us with an asynchronous version of `require()`, aptly named `require.async()`. Below is the sample example previously shown for our _utilities_ module, however non blocking. `require.async()` accepts a callback of which the first parameter `err` is `null` or an instanceof `Error`, and then the module exports. Passing the error (if there is one) as the first argument is an extremely common idiom in node for async routines.
79+
80+
require.async('sys', function(err, sys){
81+
require.async('./utilities', function(err, utils){
82+
sys.p(utils.merge({ foo: 'bar' }, { bar: 'baz' }));
83+
});
84+
});

‎src/modules/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ utils.merge(a, b);
88
sys.p(a);
99

1010
var math = require('./math');
11-
require('sys').puts(require('sys').inspect(math));
1211
sys.puts(math.add(3, 1));
1312
sys.puts(math.sub(3, 1));

‎src/modules/async.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
require.async('sys', function(err, sys){
3+
require.async('./utilities', function(err, utils){
4+
sys.p(utils.merge({ foo: 'bar' }, { bar: 'baz' }));
5+
});
6+
});

0 commit comments

Comments
 (0)