javascript - RequireJS : nested require calls - Stack Overflow

I have a scenario where I have nested require() calls to load up different modules. Is there a way for

I have a scenario where I have nested require() calls to load up different modules.

Is there a way for me to ensure that all the require() invocation and all its child require() invocations are fully loaded before the callback function is called?

Is there a way to specify that require() calls are synchronous?

function someFunction(callback) {

  //top level require
  require([...], function(...) {

     //nested require
     require([...], function(...) {
     });

     //nested require
     require([...], function(...) {
     });

  });

  callback();
};

I have a scenario where I have nested require() calls to load up different modules.

Is there a way for me to ensure that all the require() invocation and all its child require() invocations are fully loaded before the callback function is called?

Is there a way to specify that require() calls are synchronous?

function someFunction(callback) {

  //top level require
  require([...], function(...) {

     //nested require
     require([...], function(...) {
     });

     //nested require
     require([...], function(...) {
     });

  });

  callback();
};
Share Improve this question asked Jan 23, 2013 at 19:12 glui2001glui2001 491 silver badge4 bronze badges 2
  • Shouldn't you be using dependencies? – epascarello Commented Jan 23, 2013 at 19:14
  • 1 Yes. However we're trying to separate our stuff into separate files. For example the code above is in FileA.js. FileB.js calls someFunction() with its callback. However it's callback has a dependency on some settings that occur in the require() callbacks of FileA.js. Problem is that the asynchronous nature of require means that the callback is executed before it's dependencies in require() is set. – glui2001 Commented Jan 23, 2013 at 19:42
Add a ment  | 

2 Answers 2

Reset to default 3

You need to execute the callback in the last require(...) function:

function someFunction(callback) {
   require(['somemodule'], function(someModule) {
     // do stuff with someModule...

     // execute callback
     callback(); 
   });
}

What you also could do is to specify your dependencies with the define function.

Example:

define('somemodule', ['somedependency'], function(someDependency) {
   // return somemodule
   return {
     someProperty: someDependency.getProperty();
   };
});

function someFunction(callBack) {
   var someModule = require('somemodule');   
   var foo = someModule.someProperty;

   return callBack(foo);
}

There is a way to make require call sync. Make it CommonJS style:

var module = require('modulepath')

So, if you would NOT need a factory funciton in your nested require calls, you could "sync" the require calls that way... But since you do you are out of luck.

AMD style requre(depsArray, factoryFn) is exactly like pushing your code into a parallel thread. There is no way to make it "sync" but you can use "semaphores" to coordinate the result.

The answer to your question also greatly depends on what nested A and nested B consume. If they depend on some product from top require like so you absolutely must use "threaded semaphores" and cannot just push nested require calls into named define calls:

function someFunction(callback) {

  var resultOfOuterCode = someResultOfCalculations

  //top level require
  require([...], function(...) {

    var resultOfTopRequireCode = someOtherResultOfCalculations

    var semaphore = {
      'count': 2 // represents the number of thread that need to be "done" before 
      , 'callback':callback // this callback is fired.
      , 'checkIfLast': function(){
        this.count -= 1
        if (!this.count) {
          // count is now 0 - time to run our callback
          this.callback()
        }
      }
    }

    //nested require A
    require([...], function(...) {
      // using resultOfTopRequireCode // <-- !!!! this is important part
      ...
      semaphore.checkIfLast()
    });

    //nested require B
    require([...], function(...) {
      // using resultOfTopRequireCode // <-- !!!! this is important part
      semaphore.checkIfLast()
    });

  });

};

In other words, just think of require(dependsArray, factoryFn) as "threads" and apply your understanding of using threads to it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744242380a4564765.html

相关推荐

  • javascript - RequireJS : nested require calls - Stack Overflow

    I have a scenario where I have nested require() calls to load up different modules. Is there a way for

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信