Browse Source

If there's a callback, run factory async.

Herby Vojčík 4 years ago
parent
commit
c6fae6a513
2 changed files with 9 additions and 8 deletions
  1. 3 3
      README.md
  2. 6 5
      amdefine.js

+ 3 - 3
README.md

@@ -134,13 +134,13 @@ execute and trace dependencies and call the factory function *synchronously*,
 to keep the behavior in line with Node's synchronous dependency tracing.
 
 The exception: calling AMD's callback-style require() from inside a factory
-function. The require callback is called on process.nextTick():
+function. Then require and the callback is called on process.nextTick():
 
 ```javascript
 define(function (require) {
     require(['a'], function(a) {
-        //'a' is loaded synchronously, but
-        //this callback is called on process.nextTick().
+        //both 'a' is loaded, and this callback is called
+        //on process.nextTick().
     });
 });
 ```

+ 6 - 5
amdefine.js

@@ -118,14 +118,15 @@ function amdefine(module, requireFn) {
             } else {
                 //Array of dependencies with a callback.
 
-                //Convert the dependencies to modules.
-                deps = deps.map(requireInContext);
-
-                //Wait for next tick to call back the require call.
                 if (callback) {
+                    //Wait for next tick to call back the require call.
                     process.nextTick(function () {
-                        callback.apply(null, deps);
+                        //Convert the dependencies to modules.
+                        callback.apply(null, deps.map(requireInContext));
                     });
+                } else {
+                    //Require the dependencies' moduies.
+                    deps.forEach(requireInContext);
                 }
             }
         }