Background
I want to navigate to multiple websites in separate browser tabs programmatically. Ideally, I want to be able to have these websites loading in parallel, when the code is triggered.
Problem
As a proof of concept, I have the following code to open a browser through a command.
const { exec } = require("child_process");
exec('xdg-open https://www.google.com').on('exit', () => {console.log('child 1 exited')})
exec('xdg-open https://www.github.com').on('exit', () => {console.log('child 2 exited')})
What happens when I run the code above:
- Both
child 1 exited
andchild 2 exited
were logged almost immediately after I run the code - A browser tab opens and starts loading one of the pages. I had to wait about 3 seconds after the first page is loaded, before the 2nd tab opens and starts loading the second page.
++
I expected both tabs to open and start loading the pages almost at the same time, but this is not happening.
Other Observations
I tried running xdg-open https://www.google.com
and xdg-open https://www.github.com
in separate terminals (executing both at around the same time), and I am able to have 2 tabs open and start loading pages at around the same time (this is the behavior I am aiming for)
However, the code above seems to behave similarly to running xdg-open https://www.google.com && xdg-open https://www.github.com
Question
If exec()
spawns a new child_process, why is the code above not triggering multiple page navigations simultaneously? How do I achieve this behavior?