简体   繁体   English

如何设置NodeJS在退出之前等待事件循环为空?

[英]How to set NodeJS to wait for the event loop to be empty before exiting?

I'm writing a simple JS file that executes a bunch of promises that I don't care to know their state.我正在编写一个简单的 JS 文件,它执行一堆我不想知道他们的 state 的承诺。 If I start node like this: node index.js node will exit as soon as it finished firing the promises, while they still are doing things in the background.如果我这样启动节点: node index.js节点将在完成触发承诺后立即退出,而它们仍在后台执行操作。

Is it possible to pass a parameter to node and tell it to wait until the event loop is empty before it exiting?是否可以将参数传递给node并告诉它等到事件循环为空后再退出?

I did check node --help but did not find anything that could be the option that I'm looking for.我确实检查了node --help但没有找到任何可能是我正在寻找的选项。

IMPORTANT: I'm not looking for a code solution, but a parameter to pass to node itself.重要提示:我不是在寻找代码解决方案,而是在寻找传递给节点本身的参数。

Is it possible to pass a parameter to node and tell it to wait until the event loop is empty before it exiting?是否可以将参数传递给节点并告诉它等到事件循环为空后再退出?

No, there is no such parameter.不,没有这样的参数。 The desired behaviour is the default in node, it cannot be changed.所需的行为是节点中的默认行为,无法更改。 Node does exit the event loop when the event queue is empty and there are no running asynchronous tasks.当事件队列为空且没有正在运行的异步任务时,Node 会退出事件循环。

Something is broken in the code if it is still doing things in the background but didn't register the task.如果代码仍在后台执行操作但未注册任务,则代码中有问题。

I don't think there is such a feature that you can make use of to achieve the expected result.我不认为有这样一个功能可以用来实现预期的结果。 But, if you want your node process to wait until the promises are finished you can await for them.但是,如果您希望您的节点进程等到 Promise 完成,您可以await它们。

For any timers, nodejs provides ways to cancel them ( Doc ).对于任何计时器,nodejs 提供了取消它们的方法( Doc )。

If you want to wait for your timers to execute then you can maintain a global variable to track states of a timer.如果要等待计时器执行,则可以维护一个全局变量来跟踪计时器的状态。

Your code needs to be doing something in order for node to keep running.您的代码需要做一些事情才能让节点继续运行。 One of the things it could be doing is simply waiting for other code to complete.它可以做的一件事就是等待其他代码完成。 There are many ways that can be done.有很多方法可以做到。 A simple example might look something like this:一个简单的示例可能如下所示:

const resolveLater = () => {
  return new Promise(resolve => setTimeout(resolve, 5000));
}

resolveLater().then(() => console.log('Done'));

If you need multiple events, the easiest thing to do might be to do a single asynchronous function that waits for them all.如果您需要多个事件,最简单的方法可能是执行单个异步 function 等待所有事件。 Eg例如

const resolveLater = (delay) => {
  return new Promise(resolve => setTimeout(resolve, delay));
}

const resolveAll = async () => {
  await Promise.all([
    resolveLater(1000),
    resolveLater(2000),
    resolveLater(5000),
  ]);
}

resolveAll().then(() => console.log('Done'));

You don't have to do anything with the promises other than wait for them to complete .除了等待它们完成之外,您无需对承诺做任何事情。 You do care about their state, even if you don't care about their results.您确实关心他们的 state,即使您不关心他们的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM