如何在解决承诺的同时保持 node.js 脚本的活动? [英] How to keep a node.js script alive while promises are being resolved?

查看:35
本文介绍了如何在解决承诺的同时保持 node.js 脚本的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本使用承诺(使用 q 库)执行一些异步任务.运行 mocha 测试工作正常.但是,从命令行运行脚本不会.节点进程立即死亡.

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.

var bot = require('./bot');

bot.getCategories().then(function (categories) {
  console.log('Found ' + categories.length + ' categories');
});

推荐答案

我的脚本使用承诺(使用 q 库)执行一些异步任务.运行 mocha 测试工作正常.但是,从命令行运行脚本不会.节点进程立即死亡.

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 环境不应在事件循环中仍有排队的事情时过早退出.

This is most certainly a bug, please do report it. The Node.js environment should not exit prematurely while there are things still queued in the event loop.

您不应该为了发生这种情况而稍微改变您的代码.Q 库(请记住,今天有更多现代和本地替代方案)在 process.nextTick微任务"队列上安排异步回调.您的机器人库大概也执行 IO,这两件事都应该导致节点不终止.

You should not have to alter your code one bit for this to happen. The Q library (keep in mind there are more modern and native alternatives today) schedules async callbacks on the process.nextTick "microtask" queue. Your bot library presumably also performs IO, both these things should cause node not to terminate.

这篇关于如何在解决承诺的同时保持 node.js 脚本的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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