Returning the response from an asynchronous call


If you are facing any issue while a function is returning a response from an asynchronous call, then here you will get the right solution for it. In this article, we are going to tell you about the synchronous and asynchronous call.

Problem Statement:

Ajax stands for Asynchronous JavaScript and XML. It helps in creating better, faster and responsive web applications. Asynchronous in Ajax refers to sending or receiving the response which is taken out without blocking other operations. To understand it properly, you have to know the difference between Asynchronous and Synchronous.


If you are making a phone call to your friend and ask him a question, there is a possibility that he will take a little time. You may like to stare on the wall until he gives the answer that you want. This is what happens when you call a function with normal code.

function findItem() { var item; while(item_not_found) { // search } return item; } var item = findItem(); // Do something with item doSomethingElse();


Now, imagine that you have called your friend for the same reason as above. But this time, you want an answer quickly so you cut the call after telling him to call back. You can hang the call and move on to doing something that you want. Suddenly, he calls you back and gives you the required information. A similar thing happens in the case of Ajax request.

findItem(function(item) { // Do something with item }); doSomethingElse();

Here, you don’t have to wait for the response, as it is sent immediately and the executions continue. Also, the statement after the Ajax call is executed too. To get the response in the end, you have to supply a function for calling after the response will be received. Whatever statement that will come after this call will be executed before the calling of the callback.


We hope that you have learned the difference between the asynchronous and synchronous flow. Now, you can learn the solution. Sometimes, an asynchronous operation can give a synchronous counterpart that’s why it is recommended to not use it. Try to avoid it especially in the conditions of the browser.

Ever wondered why it is bad to ask so?

JavaScript operates in the UI thread of the browsers. If any process will run for a long time, then this will lock the UI. It will make the page unresponsive. Moreover, the execution time for JavaScript has an upper restriction. The browser will then ask the user if it should continue the execution or stop it. This provides bad user experience and the user won’t be able to figure out that everything is fine or there is any problem. If a user is on a slow connection, it will make the condition worse. To deal with all these issues, you can follow the given solutions-

  • Promises with async/await (ES2017+, it is available in older browsers but only when you use a transpiler or regenerator)
  • Callbacks (popular in node)
  • Promises with then() (ES2015+, available in older browsers if you use one of the many promise libraries)
  • All these solutions built on top of each other are available in current browsers, and node 7+.

Solution Worked 0 UpvotesUpvote


Solution Didn't Worked 0 DownvotesDownvote


Get Programming Help

Play 2048 Game Online

Search Tags