正确在node.js中导出异步函数 [英] Correct async function export in node.js

查看:227
本文介绍了正确在node.js中导出异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码的自定义模块:

I had my custom module with following code:

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

如果在模块外部调用该函数,效果很好,但是如果在内部调用该函数,则在运行时会出错:

It worked fine if call the function outside my module, however if I called inside I got error while running:

(节点:24372)UnhandledPromiseRejectionWarning:未处理的诺言 拒绝(拒绝ID:1):ReferenceError:PrintNearestStore不是 定义

(node:24372) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: PrintNearestStore is not defined

当我将语法更改为:

module.exports.PrintNearestStore = PrintNearestStore;

var PrintNearestStore = async function(session, lat, lon) {

}

它在模块内部开始正常工作,但在模块外部失败-我收到错误消息:

It started to work fine inside module, but fails outside the module - I got error:

(节点:32422)UnhandledPromiseRejectionWarning:未处理的诺言 拒绝(拒绝ID:1):TypeError:mymodule.PrintNearestStore为 不是功能

(node:32422) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: mymodule.PrintNearestStore is not a function

所以我将代码更改为:

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

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

现在它可以在所有情况下使用:内部和外部.但是是否想了解语义,以及是否有更美丽,更短的书写方式?如何正确定义和使用异步功能:内部(外部)模块?

And now it works in all cases: inside and outside. However want to understand semantics and if there is more beautiful and shorter way to write it? How to correctly define and use async function both: inside and outside (exports) module?

推荐答案

与异步功能无关,这实际上没有任何意义.如果要在内部调用函数并 导出,请先 定义它,然后将其导出.

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) {
...
}

这未在模块中定义功能.函数定义是函数 expression .函数表达式的名称仅在函数本身内部创建一个变量.更简单的例子:

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`

另请参见已解密的命名函数表达式.如果到处都引用module.exports.PrintNearestStore,当然可以引用该函数.

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) {

}

这几乎是 确定.问题在于,将PrintNearestStore的值分配给module.exports.PrintNearestStore时,其值为undefined.执行顺序为:

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

一个简单的例子:

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) {
...
}

之所以可行,是因为到时候分配给module.exports.PrintNearestStore 的功能已执行PrintNearestStore的功能就是它的值.

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

一个简单的例子:

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

这篇关于正确在node.js中导出异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆