Why doesn't ElectronJS simply exit when given a blank index.js file?

Viewed 41

I'm trying to understand the Electron Main process. I'm confused about something.

Try this:

% touch blank.js
% electron blank.js

You will notice that electron does not exit. Since no 'app' has been created, it isn't clear to me why the process is sticking around, and why I need to call process.exit from blank.js to terminate. The documentation is a bit thin in describing main/browser extensions to process.

1 Answers

Say I have a typical electron app:

await app.whenReady();
const win = new BrowserWindow();
await win.loadUrl("https://google.com");

The window is created and the main method exits. Nothing else is awaiting anything. Electron doesn't exit in this case, because it doesn't make sense to since we're now just waiting on the user to interact with the window.

The things that should trigger an exit are the remaining window closing (window-all-closed event), or an explicit app.quit() type of command.

Now I guess you might say that this is different since a window actually got created, but how would Electron know that this will happen? For how long must it wait until it can be sure that no window will be created?

Establishing a rule like "the main method must await the window creation" seems overly-restrictive to me, so Electron just assumes that it might happen later or it might not, but until it's told to exit, it'll wait.

Ultimately, someone on the Electron team could give a more accurate answer, but this is my best guess.

Related