为什么使用 Promises 的节点应用程序不能 return?
Why might a node app using Promises not return?
我在 CLI 应用程序中使用 Bluebird。
由于某种原因,应用程序没有完成,但我不明白为什么。我已经尝试 运行 调试(在 Webstorm 中)并在 "hung" 时暂停,但我没有得到任何信息。
代码的形式是:
Promise.all([ /* promises */ ])
.then(function () {
return Promise.all([ /* promises */ ]);
})
.then(function () {
return console.log("Done");
})
.catch(function (err) {
return console.error("Failed: " + err);
});
该应用程序报告 Done
,并且通过检查我的数据库(因为承诺来自 SQL INSERT),我可以看到所有内容。
我正在使用
mysql.createPool();
来自 promise-mysql
发送我的查询。
如何调试这个挂起?或者是否有我可以探索的常见故障模式?
你能加吗
.then(null, function (error) {
return console.log(error);
})
也许它失败了,你没有抓住这个案例
听起来您可能没有关闭所有 mysql 连接或没有终止池。如果您还没有这样做,明智的做法是在 mysql 操作之后在 .finally()
中处理它:
pool.end();
如 docs 中所示(强调已添加):
When you are done using the pool, you have to end all the connections or the Node.js event loop will stay active until the connections are closed by the MySQL server. This is typically done if the pool is used in a script or when trying to gracefully shutdown a server. To end all the connections in the pool, use the end method on the pool
我在 CLI 应用程序中使用 Bluebird。
由于某种原因,应用程序没有完成,但我不明白为什么。我已经尝试 运行 调试(在 Webstorm 中)并在 "hung" 时暂停,但我没有得到任何信息。
代码的形式是:
Promise.all([ /* promises */ ])
.then(function () {
return Promise.all([ /* promises */ ]);
})
.then(function () {
return console.log("Done");
})
.catch(function (err) {
return console.error("Failed: " + err);
});
该应用程序报告 Done
,并且通过检查我的数据库(因为承诺来自 SQL INSERT),我可以看到所有内容。
我正在使用
mysql.createPool();
来自 promise-mysql
发送我的查询。
如何调试这个挂起?或者是否有我可以探索的常见故障模式?
你能加吗
.then(null, function (error) {
return console.log(error);
})
也许它失败了,你没有抓住这个案例
听起来您可能没有关闭所有 mysql 连接或没有终止池。如果您还没有这样做,明智的做法是在 mysql 操作之后在 .finally()
中处理它:
pool.end();
如 docs 中所示(强调已添加):
When you are done using the pool, you have to end all the connections or the Node.js event loop will stay active until the connections are closed by the MySQL server. This is typically done if the pool is used in a script or when trying to gracefully shutdown a server. To end all the connections in the pool, use the end method on the pool