我的脚本使用 Promise(使用 q 库)执行一些异步任务。运行 mocha 测试工作正常。但是从命令行运行脚本不会。 Node 进程立即死亡。
var bot = require('./bot');
bot.getCategories().then(function (categories) {
console.log('Found ' + categories.length + ' categories');
});
最佳答案
My script performs some asynchronous tasks using promises (with the q library). Running mocha tests works fine. However running the script from the command line does not. The node process immediately dies.
这肯定是一个错误,请报告它。 Node.js 环境不应在事件循环中仍有排队的情况下过早退出。
您不应该为了发生这种情况而稍微更改您的代码。 Q 库(请记住,今天有更多现代和原生的替代方案)在 process.nextTick
“微任务”队列上安排异步回调。您的机器人库大概也执行 IO,这两件事都应该导致 Node 不终止。
关于node.js - 在解决 promise 时如何保持 node.js 脚本处于事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32585427/