Exit NodeJS script when all asynchronous tasks are done

I am doing some asynchronous process with firebase using NodeJS.

I would stop when I finished all the tasks of completing the NodeJS process without the need for Ctrl + C command.

I tried to exit the process, but it works before all execution is done.

How can I complete all asynchronous tasks and then exit the script?

+3


source to share


1 answer


First, all your asynchronous processes must be promises, then you wrap all those promises into one promise with a help Promise.all

and exit when that promise is resolved. Like this:



Promise.all([

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

]).then(process.exit);

      

+5


source







All Articles