you’re on your way 4
37
next generation applications
You’ve uncovered the trickiest part of
asynchronous web programming: if
asynchronous code doesn’t wait around for
a response from the server, how do we use
that response? And where does the server’s
response go in the rst place?
You already know about web pages,
which can use event handlers to call
JavaScript functions. And you’ve seen
how asynchronous JavaScript can make
requests to a web server. To complete the
asynchronous picture, though, there’s
another important piece of the puzzle...
Where does the response go?
So if getBoardsSold() nishes running
before the server responds, where does
the response go? And how can we make sure
that updatePage() runs when the server’s
nished with our request?
Remember, getBoardsSold() makes
an asynchronous request. That means that
the JavaScript doesn’t wait around for an
answer from the server.
In fact, the getBoardsSold() function
will probably nish running before the
getUpdatedBoardSales-ajax.
php script even has a chance to nish
processing your request.
Ajax is asynchronous JavaScript
I think we should call the updatePage()
function at the end of getBoardsSold().
Then updatePage() can get the response
from the server, right?
function getBoardsSold() {
createRequest();
var url =
“getUpdatedBoardSales-ajax.php”;
request.open(“GET”, url, true);
request.send(null);
updatePage();
}
This won’t ...