— Asynchronous over Synchronous
Have you encountered any performance issues recently?
Is the spinning icon troubling you?
Any timeout errors that you wish you could solve?
Well, I found answers to some of these issues using Asynchronous Requests.
Before we move further on how asynchronous requests can handle all the above issues, let me give you a brief on synchronous and asynchronous requests.
Synchronous Request: A Synchronous request waits for the response from that request and will not execute the next one till the response is returned. i.e when a client makes a call synchronously it stops the client browser to make certain that it does not make another call till it gets the response from the call that was fired prior to that.
Asynchronous Request: An asynchronous call, on the other hand, works exclusively, which means, unlike synchronous, it does not wait for any response to be returned to execute another. This implies that you can make any number of calls, anytime without having to wait for a response. Simply “fire and forget”. Curious to know how an asynchronous method got this name, I googled and gathered a piece of interesting information.
Here’s what I found
Fire and Forget is a type of missile guidance which does not require further guidance after launches such as illumination of the target or wire guidance, and can hit its target without the launcher being in line-of-sight of the target.
What better name could there be?
Asynchronous calls allow the user to interact with a page without any delay as the process requested shall be running in the background, therefore, the page doesn’t need to reload. Whereas, synchronous calls stop the visitor from interacting with any other elements in the page until the response is received and the page is updated.
Now that we got a brief on synchronous and asynchronous methods.
Let’s move on to the ways to tackle different issues related to performance using Async.
Case 1:
Based on a common observation, for issues where your browser hangs or appears to be frozen, check the ajax asynchronous call.
By default, ajax is an asynchronous call, you can make it as synchronous call by using async: false.
Check if the asynchronous request is false, this would be the reason your browser hangs while loading a result. Make the async: true and this should solve the problem. For features where you have to disallow user interaction until a result is fetched, go ahead with the synchronous request. (for example a transaction confirmation)
Case 2:
Let me show you a second case, where async worked for me. We were building an application for online classes that involved a third-party Learning Management tool. We hit a roadblock where the user experience was greatly affected. The user assigns a grader to a student and had to wait till we received the confirmation from the third party to assign another student. This would usually take 75 sec. For issues where it takes a long time to fetch a result, which keeps the user waiting, the best way to tackle it is by using fire away async methods (reminds of the song by Chris Stapleton?). This initiates the request as a background process and the user meanwhile shall be free to interact.
We made a fire away call to the function asynchronously using Task. Run (without await).
The Run method allows you to create and execute a task in a single method call. One way to turn the asynchronous operation into an asynchronous one is to run it on a separate thread, and that’s where Task. Run comes in.
“fire and forget” — that you don’t care about when it completes or whether it completes successfully or with error — which is extremely rare. Task. Run with await can be used if you had to be sure of what it returned or catch any exceptions and then proceed.
Final Thoughts:
Initially, it took some time to ascertain certain ambiguities related to the application, but later it’s gotten a lot easier. The only way you can know for sure about the impact is to build a realistic test and measure it.
Once you are used to it, it is an incredibly useful tool to make your life easier as your application grows complex.
“Continuous improvement is better than delayed perfection”-Mark Twain