Most beginner resources will teach you the following when sending asynchronous requests:
- Send a request
- Wait for a response
- Update the UI
These steps are useful when you send the first request since you need to fetch resources before showing the interface to users.
But if you do the same steps in later requests, you force a user to wait for you to save the data — this can be frustrating from a user experience point of view.
For example, imagine you have a Todolist.
When you add items to this Todolist:
- You don’t want to wait for the items to be saved into the server first.
- You just want to add items.
- If the code forces you to wait before adding each item, you will become frustrated.
Instead of forcing users to wait, a better approach is to use Optimistic UI.
What is Optimistic UI?
Optimistic UI assumes the request will be successful, which means you can update the interface immediately. This is okay because requests are generally going to be successful.
If the request fails, you revert the UI back to the original state before the changes were made.
Most web applications use Optimistic UI because it provides a better user experience.
One example of Optimistic UI is Google Doc’s autosave feature — in this case, Google Docs saves your work automatically in the background without interrupting you when you work.
You can see other examples of Optimistic UI here.
I teach you how to write Optimistic UI code in our sister course, Learn JavaScript. Click this link if you want to learn how.
With that, you have completed a firm understanding of the fundamental JavaScript you must know. you’re now ready to learn about Rest APIs.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson