When a JavaScript developer has to store data that persists on their webpage after a refresh, they have to branch out from simple DOM manipulation. A lot of JS developers end up using AJAX, which stands for Asynchronous JavaScript and XML. AJAX uses JavaScript to send a HTTP request to the website’s database. The database will follow the instructions given to it in the HTTP request, and then will respond to the client’s browser with the new information.

How do we go about rendering this new information? You might think the next logical step is to use the data that our client’s browser just received and display it, but some websites actually don’t do that. There are two different schools of thought on how a webpage should render this new data. What I just described, where the site renders the change using the data from the database’s response, is called pessimistic rendering. Optimistic rendering is when the site renders the changes in the client’s browser first, and then makes the request to the database. Optimistic rendering wants to believe that our database request will go through, and so it acts before the client sends the request. Pessimistic rendering is more cautious; it only modifies the webpage based off the info they directly get back from the database.

Image for post

How a Like is Rendered Optimistically vs Pessimistically

#rendering #javascript #ajax #programming #dom

Believing in Your Requests
1.20 GEEK