IndexedDB onsuccess/onfailure event listener: why assign it after, not before, the open action?

For example

let db;

// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

// these event handlers act on the database being opened.
DBOpenRequest.onerror = (event) => {
  note.innerHTML += "<li>Error loading database.</li>";
};

DBOpenRequest.onsuccess = (event) => {
  note.innerHTML += "<li>Database initialized.</li>";
};

What I understand is that DBOpenRequest.onerror = xxx is setting an event listener to handle the event error.

But why we set the event handler after the event emitting statement (i.e. the open action const DBOpenRequest = window.indexedDB.open) is executed, not before?

Isn’t it possible that after the the event emitting statement is executed and before we set the error event handler, the event is already emitted, so that it is never captured by any event handler (since there is none at all)?