Michael
Michael

Reputation: 59

why is requirejs on node not loading the require specified before continuing

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'?

Upvotes: 1

Views: 192

Answers (1)

Louis
Louis

Reputation: 151391

What you are seeing is exactly what is expected. Here is the sequence of events:

  1. requirejs([ 'part1'], tells RequireJS to initiate the loading of part1. The loading will complete some time in the future.

  2. RequireJS fetches the code of part1 and executes it. It finds an anonymous define which RequireJS interprets as being the definition of part1 so it executes the callback passed to the define.

  3. The line fs.readFile is executed. This initiates the reading the file. The operation will complete some time in the future. This is asynchronous.

  4. The execution of the callback passed to define continues and completes. If you had a console.log statement after fs.readFile, you'd see its output right there before the contents of the file.

  5. Since execution of the callback passed to define is done, RequireJS then can call the callback that was passed to requirejs([ 'part1'],. There is no reason this must be delayed. In particular, the fs.readFile operation has no bearing on this.

  6. The file is eventually read and the callback passed to fs.readFile is called.

If you used fs.readFileSync then you'd get the behavior you are expecting.

Side note: using this.text in your code is probably not doing what you think it is doing. At that point in your code this === global.

Upvotes: 1

Related Questions