JavaScript shutting the stable door after the horse has bolted?

Anyone up for some JavaScript shenanigans? Take a look at this:

<!doctype html>
<html lang="en">
<button onclick="pick()">Choose a folder</button>
<script>
async function pick() {
  const folder = await showDirectoryPicker();
  const open = indexedDB.open("demo", 1);
  open.onupgradeneeded = () => open.result.createObjectStore("handles"); // first time: make the store
  open.onsuccess = () => open.result.transaction("handles", "readwrite").objectStore("handles").put(folder, "folder");
}
</script>
</html> 

It’s a web page that contains a tiny piece of JavaScript that creates a handle for a local filesystem and stores it in an indexedDB when you press a button with the caption “Choose a folder”. Why do we need this? Well, suppose you have a web application that wants to store data on the local machine. Something that is not usually allowed, what with all the nasty sites out there. Well, the application can ask for a handle to a folder on your machine that you are happy for the browser to work with. That’s what showDirectoryPicker does. It asks the user to pick a folder where the browser can put stuff for this site. I don’t want the user to have to do this every time they use my web based application. So I need to store it locally.

I can use a local indexedDB to do that. This is not really a database, it’s more of an object store. However, it is terribly useful. So I’ll open the database and add the folder reference to the database. That’s what the code above does. But it contains some code that hurt my head for a while:

  const open = indexedDB.open("demo", 1);
  open.onupgradeneeded = () => open.result.createObjectStore("handles");// first time: make the store
  open.onsuccess = () => open.result.transaction("handles", "readwrite").objectStore("handles").put(folder, "folder");

The first statement opens a connection to a database called demo. It wants version 1. Fair enough. The second statement binds a function to the onupgradeneeded event on the connection. This fires when the database version is wrong or if the database is not there. The handler creates a new database called handles. So, if we ask for a database and it isn’t there, we make one. Lovely. The next statement creates a handler for the “happy path” - otherwise known as onsuccess. This is the code that runs once the database connection is open. When that happens we want to make an object called “folder” which contains the folder we created earlier. That’s what the transaction does. Now, when the website loads all my application has to do is look in the local database for the handle so it can use local files. Also lovely. All the code makes sense. So why did it make my head hurt?

Well, it’s because when I first looked at it I thought we were closing the stable door after the horse has bolted. The first statement does the open. And the, after that, we put two event handlers into place to deal with what happens when the open runs. At first sight this seems wrong. How do the handlers get called when they aren’t set up? But it makes sense. And it is all to do with how JavaScript does things.

The first thing you need to know is that indexedDB.open doesn’t open the database. It starts opening the database. So when open returns the database isn’t open. It’s being opened.

The second thing you need to know is that open won’t call the event handlers when it wants to fire the events. It will put the events into an ordered list to be dealt with later. So the fact that it might not have anything assigned to deal with the events isn’t a problem.

The third thing you need to do is when JavaScript finishes the code that was called when the pick button was pressed it then goes through the list of events that have been raised and calls each one in turn. This happens when the event handlers have been assigned, so they all run and do what we want.

This makes perfect sense. In the code above the user presses the “Choose a folder” button. This calls the pick function which pops up the file picker, captures the folder and then pops it in the database. We don’t want our button handler to have to hang around waiting for the database to catch up. We want it to return to the user so that they can do other things. When the database does catch up, the events need to fire and sort everything out.

JavaScript is super clever. But it can also be super-confusing sometimes.