3.22. Writing the callback functionThe only function left to write is serveDrink(), which needs to take the response from the server and figure out who placed the order, and which coffee maker was used to brew the order. Then serveDrink() can set the coffee maker's status to "Idle" and let the person who placed the order know that their coffee is ready. Let's start by checking the ready state, the HTTP status code, and then getting the response from the server. function serveDrink() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; // Figure out who placed the order, and // which coffee maker was used } else alert("Error! Request status is " + request.status); } } All of this code should be pretty familiar by now. You'll use this same code in almost every callback function you ever write. |