Languages
[Edit]
EN

JavaScript - return ajax response from asynchronous call

11 points
Created by:
Lilly-Grace-Greig
571

In this article, we would like to show you how to return an ajax response from an asynchronous call in JavaScript​​​​​.

1. Overview

Edit

For making a request with AJAX it is necessary to make an async operation - the sync AJAX request technic is not recommended in the main thread for many web browsers and is marked as deprecated. Async operation requires callback methods or async/await keywords usage. The callback method is executed in the event loop, which means the response is not available during sequential logic execution that contains AJAX request - we need to wait until request logic will be finished and return to the JavaScript program response.

1.1 Common source code example

Edit

Each below AJAX example (from 2nd to 4th) has been executed with the below Java Spring backend logic.

EchoController.java file:

2. jQuery.ajax ($.ajax) method example

Edit

2.1. Callback method example

Edit

index.html file:

Note: ajax.html and backend.php files should be placed on php server both.

Result:

jQuery.ajax method with callback example
jQuery.ajax method with callback example

2.2. Promise with then/catch method example

Edit

index.html file:

Note: ajax.html and backend.php files should be placed on php server both.

Result:

jQuery.ajax Promise with then/catch method example
jQuery.ajax Promise with then/catch method example

2.3. Promise with async/await keywords example

Edit

index.html file:

Notes:

  • await operations can be executed only in async methods - this way makes an impression the operation looks like assigning returned value in sync programming.
  • ajax.html and backend.php files should be placed on php server both.

Result:

jQuery.ajax Promise with async/await keywords example
jQuery.ajax Promise with async/await keywords example

3. XMLHttpRequest class example

Edit

3.1. Callback method example

Edit

index.html file:

Note: ajax.html and backend.php files should be placed on php server both.

Result:

XMLHttpRequest with callback method example
XMLHttpRequest with callback method example

3.2. Promise with then/catch method example

Edit

index.html file:

Note: ajax.html and backend.php files should be placed on php server both.

Result:

XMLHttpRequest Promise with then/catch method example
XMLHttpRequest Promise with then/catch method example

3.3. Promise with async/await keywords example

Edit

index.html file:

Notes:

  • await operations can be executed only in async methods - this way makes an impression the operation looks like assigning returned value in sync programming.
  • ajax.html and backend.php files should be placed on php server both.

Result:

XMLHttpRequest Promise with async/await keywords example
XMLHttpRequest Promise with async/await keywords example

4. fetch method example

Edit

4.1. then/catch method example

Edit

index.html file:

Note: ajax.html and backend.php files should be placed on php server both.

Result:

fetch with then/catch method example
fetch with then/catch method example

4.2. async/await keywords example

Edit

index.html file:

Notes:

  • await operations can be executed only in async methods - this way makes an impression the operation looks like assigning returned value in sync programming.
  • ajax.html and backend.php files should be placed on php server both.

Result:

fetch async/await keywords example
fetch async/await keywords example

Alternative titles

  1. JavaScript - how to return ajax response from asynchronous call?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join