Exporting Node module from promise result

后端 未结 2 2025
谎友^
谎友^ 2020-11-29 10:39

I\'m trying to rewrite a module to return a different value than before, but now it uses an async call to get that value. (with child_process if it matters). I\

相关标签:
2条回答
  • 2020-11-29 10:56

    Modules in Node that you load with require() are loaded synchronously and it is not possible for require to return any value that is loaded asynchronously. It can return a promise but then users of that module would have to use it as:

    require('module-name').then(value => {
      // you have your value here
    });
    

    It would not be possible to write:

    var value = require('module-name');
    // you cannot have your value here because this line
    // will get evaluated before that value is available
    

    Of course you can have the promise resolved inside of your module and make it set a property on the exported object by adding something like this:

    module.exports = { GEM_HOME: null };
    

    and changing:

    module.exports = GEM_HOME
    

    to:

    module.exports.GEM_HOME = GEM_HOME
    

    In that case, every other module that uses this module as:

    var x = require('module-name');
    

    will have x.GEM_HOME originally set to null but it would eventually get changed to a correct value some time later. It would not be available right away though, because require() returns before the promise is settled and the value is set.

    There is an ongoing discussion to introduce asynchronous module loading with different syntax and semantics that may be suited for your use case. It's a controversial subjects and it's worth reading all of the rationale behind it - see:

    • Node.js, TC-39, and Modules by James M Snell from iBM
    • ES6 Module Interoperability - Node.js Enhancement Proposals
    • In Defense of .js - A Proposal for Node.js Modules by Dave Herman, Yehuda Katz and Caridy Patiño
    • Discussion on the Pull Request #3 of node-eps (002: ES6 module interop)

    See also this answer for more details:

    • javascript - Why is there a spec for sync and async modules?
    0 讨论(0)
  • 2020-11-29 11:19

    Node.js modules are loaded synchronously.

    You can deal with this exporting the Promise value.

    #@ your module.js
    module.exports = new Promise()
    

    and:

    #@ your app.js
    const mod = require('./module');
    
    mod.then((value => ...);
    
    0 讨论(0)
提交回复
热议问题