Correct async function export in node.js

前端 未结 2 1616
自闭症患者
自闭症患者 2020-12-24 00:48

I had my custom module with following code:

module.exports.PrintNearestStore = async function PrintNearestStore(session, lat, lon) {
...
}

相关标签:
2条回答
  • 2020-12-24 01:18

    Error with first case: PrintNearestStore - Function expression, so this name not available outside.

    error with second case: using variable, instead Function declaration. In this case, declaration of variable PrintNearestStore are hoisted, so, you can use this name before line var PrintNearestStore = ..., but in this case value would be undefined.

    So, simplest solution change second variant like this:

    module.exports.PrintNearestStore = PrintNearestStore;
    
    async function PrintNearestStore(session, lat, lon) {
    
    }
    
    0 讨论(0)
  • 2020-12-24 01:22

    This doesn't really have anything to with async functions specially. If you want to call a function internally and export it, define it first and then export it.

    async function doStuff() {
      // ...
    }
    // doStuff is defined inside the module so we can call it wherever we want
    
    // Export it to make it available outside
    module.exports.doStuff = doStuff;
    

    Explanation of the problems with your attempts:

    module.exports.PrintNearestStore = async function PrintNearestStore(session, lat, lon) {
    ...
    }
    

    This does not define a function in the module. The function definition is a function expression. The name of a function expression only creates a variable inside the function itself. Simpler example:

    var foo = function bar() {
      console.log(typeof bar); // 'function' - works
    };
    foo();
    console.log(typeof foo); // 'function' - works
    console.log(typeof bar); // 'undefined' - there is no such variable `bar`

    See also Named function expressions demystified. You could of course refer to the function if you'd refer to module.exports.PrintNearestStore everywhere.


    module.exports.PrintNearestStore = PrintNearestStore;
    
    var PrintNearestStore = async function(session, lat, lon) {
    
    }
    

    This is almost OK. The problem is that the value of PrintNearestStore is undefined when you assign it to module.exports.PrintNearestStore. The order of execution is:

    var PrintNearestStore; // `undefined` by default
    // still `undefined`, hence `module.exports.PrintNearestStore` is `undefined`
    module.exports.PrintNearestStore = PrintNearestStore;
    
    PrintNearestStore = async function(session, lat, lon) {}
    // now has a function as value, but it's too late
    

    Simpler example:

    var foo = bar;
    console.log(foo, bar); // logs `undefined`, `undefined` because `bar` is `undefined`
    var bar = 21;
    console.log(foo, bar); // logs `undefined`, `21`

    If you changed the order it would work as expected.


    module.exports.PrintNearestStore = async function(session, lat, lon) {
        await PrintNearestStore(session, lat, lon);
    }
    
    var PrintNearestStore = async function(session, lat, lon) {
    ...
    }
    

    This works because by the time the function assigned to module.exports.PrintNearestStore is executed, PrintNearestStore has the function as its value.

    Simpler example:

    var foo = function() {
      console.log(bar);
    };
    foo(); // logs `undefined`
    var bar = 21;
    foo(); // logs `21`

    0 讨论(0)
提交回复
热议问题