Non-blocking is required in node.js

Is there a way to use require(or something else equivalent) on the server at runtime (maintenance time) without blocking all of this?

I am trying to add user-defined functions, after which he can use them in the language that I implement.

So for example:

toaprse.mylanguage

#bind somefunctions.js
x = somefunctions.func1();

I could make a simple request ("somefunction"); but I do not want to block the node, which I understand in this case.

I just want to pass functions to my framework, it should not be require, but it seems natural.

+1
source share
2 answers

Here's how it is implemented:

> console.log(require.extensions['.js'].toString())
function (module, filename) {
  var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
  module._compile(stripBOM(content), filename);
}

You can do the same in your application. I think something like this will work:

var fs = require('fs')

require.async = function(filename, callback) {
  fs.readFile(filename, 'utf8', function(err, content) {
    if (err) return callback(err)
    module._compile(content, filename)

    // this require call won't block anything because of caching
    callback(null, require(filename))
  })
}

require.async('./test.js', function(err, module) {
  console.log(module)
})
+3

Promises . Promises, , .

node, :

, ;)

+2

All Articles