简体   繁体   中英

Exit from a NodeJS script when all asynchronous tasks are done

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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