Another critical piece of any front-end application is network interactions, fetching data, issuing commands, and so forth. Since network communications are an inherently asynchronous activity, we have to rely on events—the EventTarget
interface to be precise.
We'll start by looking at the generic mechanism that hooks up our callback functions with requests and getting responses from the back-end. Then, we'll look at how trying to synchronize several network requests creates a seemingly hopeless concurrency scenario.
To interact with the network, we create a new instance of XMLHttpRequest
. We then tell it the type of request that we want to make—GET versus POST and the request endpoint. These request objects also implement the EventTarget
interface so that we can listen for data arriving from the network. Here's an example of what this code looks like:
// Callback for successful network request, // parses JSON data. function onLoad(e) { console...