Update...
Even without the async load file operation, it still doesn't run how I would expect it to based on your explanation...for example
part0_start.js...
var requirejs = require('requirejs');
requirejs.config({
nodeRequire : require
});
requirejs([ 'part1_setup'], function( part1_setup ) {//this callback executes when part1_setup has been compiled and run
console.log( 'part1_setup callback called')
})
part1_setup.js...
requirejs( [ 'stopwatchObject' ], function( stopwatch ) {//run the callback function when stopwatchObject is loaded...
console.log( 'stopwatch object compiled' );
})
stopwatchObject.js...
define ( [], function() {//this anonymous function happens straight away, go straight to the callback function on line below...
console.log( "stopwatch object");
})
I would expect this to output
stopwatch object
stopwatch object compiled
part1_setup callback called
but it actually outputs...
part1_setup callback called
stopwatch object
stopwatch object compiled
Or am I overlooking something obvious? Thanks again for your feedback.
My understanding of requirejs is that it allows me to specify module(s) to be loaded before continuing the thread, allowing me to be block if I want. It has worked this way when I've previously used it. However when I use it with node, this aspect of it no longer works. Obviously I know node is non-blocking, is this the reason why require isn't functioning like I expect it to. For example...
test.txt
test text test text test text test text test text test text test text
start.js
var requirejs = require('requirejs');
requirejs.config({
nodeRequire : require
});
requirejs([ 'part1'], function( part1 ) {
console.log( 'loaded' )
})
part1.js
define([], function() {
var fs = require('fs'), filename = 'test.txt';
fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err;
this.text = data;
console.log( this.text );
})
})
outputs in the following order...
module loaded
test text test text test text test text test text test text test text
But if I was specifying require part1.js, should it not compile this completely and log the loaded text before getting to logging 'loaded'?
requirejscall with adefinecall in yourpart1_setupyou should get the order you expect. If somehow there's still an issue, I would suggest asking a new question, because the problem in your edit is related but conceptually different from your original question.