0

Why is it possible to write in node (electron) something like this:

app.on('ready', function() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 480
    });
});

but this thows an error that app is not ready?

app.on('ready', onReady());

function onReady() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 480
    });
}
EchtFettigerKeks
  • 1,692
  • 1
  • 18
  • 33

1 Answers1

1

It's because you're confusing function reference and function call.

Note that the function onReady returns nothing. By default this means it returns undefined.

So doing this:

app.on('ready', onReady());

Leads to this:

app.on('ready', undefined);

That is, the onReady function is called and it's result is passed to app.on().

Basically what you've done is this:

app.on('ready', (function() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 480
    });
})());

What you want instead is:

app.on('ready', onReady);
slebetman
  • 109,858
  • 19
  • 140
  • 171