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

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

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

( node :24372)未处理的PromisejectionWarning:未处理的promise

当我将语法更改为:

module.exports.PrintNearestStore = PrintNearestStore;

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

}

它在模块内部开始工作正常,但在模块外部出现故障-我遇到了错误:

( node :32422)未处理的PromisejectionWarning:未处理的promise

所以我把代码改成:

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

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

现在它在所有情况下都有效:内部和外部.然而,你想理解语义学吗?如果有更漂亮、更短的方法来写它呢?如何正确定义和使用async函数:内部和外部(导出)模块?

推荐答案

这实际上与异步函数没有任何关系.如果你想在内部调用一个函数and,导出它,定义它first,然后导出它.

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;

解释您try 的问题:

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

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

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`

另见第Named function expressions demystified页.如果你在任何地方都提到module.exports.PrintNearestStore,你当然可以提到这个函数.


module.exports.PrintNearestStore = PrintNearestStore;

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

}

好的.问题是,当您将PrintNearestStore指定给module.exports.PrintNearestStore时,它的值是undefined.执行顺序如下:

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`

如果您更改了顺序,它将按预期工作.


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

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

这是因为分配给module.exports.PrintNearestStoreis executedPrintNearestStore的函数将函数作为其值.

更简单的例子:

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

Node.js相关问答推荐

使用Moongose处理node.js中重定向的then()块链

购物车是空的状态|本地存储不更新产品数量|Redux|

MongoDB如果文档S的字段小于另一个字段,则将该字段加一

Node.js中Redis的并发问题

为什么 mongoose 没有常规数组方法

使用 axios 和 Cheerio (Node js) 抓取 google 搜索

无法截取页面截图

为什么运行 yarn 命令会出错 - yargs-parser的完整性判断失败

级联定时器和Jest 的异步功能

如何调用同名的两个函数?

用户与mongoose 的完美搭配

cURL 和 shell 任务

Node JS:自动 Select `http.get`与`https.get`

socket.io 发出回调合适吗?

在 express 中添加故意延迟

如何为 node.js 服务器分配域名?

Forever + Nodemon 一起运行

nodejs:Ajax 与 Socket.IO,优缺点

当我try 向我的 S3 存储桶 (Node.js) 发送内容时,AWS 缺少凭证

我应该如何在 webpack 中使用时刻时区?