最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Exit from a NodeJS script when all asynchronous tasks are done - Stack Overflow

programmeradmin0浏览0评论

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

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?

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

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?

Share Improve this question asked May 9, 2017 at 12:46 frankfullstackfrankfullstack 5507 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

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);
发布评论

评论列表(0)

  1. 暂无评论