完成所有异步任务后退出NodeJS脚本 [英] Exit from a NodeJS script when all asynchronous tasks are done

查看:297
本文介绍了完成所有异步任务后退出NodeJS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有NodeJS的firebase执行一些异步过程。

I'm executing some asynchronous process with firebase with NodeJS.

我想在完成NodeJS流程执行的所有任务时停止而不需要Ctrl + C命令。

I'd like to stop when finish all tasks the NodeJS process execution without the need of Ctrl+C command.

我试图退出进程,但它在所有执行完成之前运行。

I tried to exit from the process, but it runs before all execution are done.

如何运行所有异步任务然后退出脚本?

How can I do to run all asynchronous tasks and then exit from the script?

推荐答案

首先,所有异步进程都应该是promises然后用 Promise.all 将所有这些承诺包装在一个承诺中,并在该承诺结算时退出。像这样:

First, all your asynchronous processes should be promises, then you wrap all of these promises in a single promise with Promise.all and exit when that promise resolves. Like this:

Promise.all([

 promiseForAsynchronousProcess1,
 promiseForAsynchronousProcess2,
 promiseForAsynchronousProcess3,
 ... and so on...

]).then(process.exit);

这篇关于完成所有异步任务后退出NodeJS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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