{"id":3829,"date":"2022-08-20T18:57:47","date_gmt":"2022-08-20T13:27:47","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/"},"modified":"2022-08-20T18:57:47","modified_gmt":"2022-08-20T13:27:47","slug":"solved-how-do-i-return-the-response-from-an-asynchronous-call","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/","title":{"rendered":"(Solved) How do I return the response from an asynchronous call?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-14220323\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"14220323\" data-parentid=\"14220321\" data-score=\"6413\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p><em>\u2192 For a more general explanation of asynchronous behaviour with different examples, see<\/em> Why is my variable unaltered after I modify it inside of a function? &#8211; Asynchronous code reference<\/p>\n<p><em>\u2192 If you already understand the problem, skip to the possible solutions below.<\/em><\/p>\n<\/blockquote>\n<h1>The problem<\/h1>\n<p>The <strong>A<\/strong> in <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Ajax_(programming)\">Ajax<\/a> stands for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.merriam-webster.com\/dictionary\/asynchronous\"><strong>asynchronous<\/strong><\/a>. That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, <code>$.ajax<\/code> returns immediately and the next statement, <code>return result;<\/code>, is executed before the function you passed as <code>success<\/code> callback was even called.<\/p>\n<p>Here is an analogy which hopefully makes the difference between synchronous and asynchronous flow clearer:<\/p>\n<h2>Synchronous<\/h2>\n<p>Imagine you make a phone call to a friend and ask him to look something up for you. Although it might take a while, you wait on the phone and stare into space, until your friend gives you the answer that you needed.<\/p>\n<p>The same is happening when you make a function call containing &#8220;normal&#8221; code:<\/p>\n<pre><code>function findItem() {\n    var item;\n    while(item_not_found) {\n        \/\/ search\n    }\n    return item;\n}\n\nvar item = findItem();\n\n\/\/ Do something with item\ndoSomethingElse();\n<\/code><\/pre>\n<p>Even though <code>findItem<\/code> might take a long time to execute, any code coming after <code>var item = findItem();<\/code> has to <em>wait<\/em> until the function returns the result.<\/p>\n<h2>Asynchronous<\/h2>\n<p>You call your friend again for the same reason. But this time you tell him that you are in a hurry and he should <em>call you back<\/em> on your mobile phone. You hang up, leave the house, and do whatever you planned to do. Once your friend calls you back, you are dealing with the information he gave to you.<\/p>\n<p>That&#8217;s exactly what&#8217;s happening when you do an Ajax request.<\/p>\n<pre><code>findItem(function(item) {\n    \/\/ Do something with the item\n});\ndoSomethingElse();\n<\/code><\/pre>\n<p>Instead of waiting for the response, the execution continues immediately and the statement after the Ajax call is executed. To get the response eventually, you provide a function to be called once the response was received, a <em>callback<\/em> (notice something? <em>call back<\/em> ?). Any statement coming after that call is executed before the callback is called.<\/p>\n<hr>\n<h1>Solution(s)<\/h1>\n<p><strong>Embrace the asynchronous nature of JavaScript!<\/strong> While certain asynchronous operations provide synchronous counterparts (so does &#8220;Ajax&#8221;), it&#8217;s generally discouraged to use them, especially in a browser context.<\/p>\n<p>Why is it bad do you ask?<\/p>\n<p>JavaScript runs in the UI thread of the browser and any long-running process will lock the UI, making it unresponsive. Additionally, there is an upper limit on the execution time for JavaScript and the browser will ask the user whether to continue the execution or not.<\/p>\n<p>All of this results in a really bad user experience. The user won&#8217;t be able to tell whether everything is working fine or not. Furthermore, the effect will be worse for users with a slow connection.<\/p>\n<p>In the following we will look at three different solutions that are all building on top of each other:<\/p>\n<ul>\n<li><strong>Promises with <code>async\/await<\/code><\/strong> (ES2017+, available in older browsers if you use a transpiler or regenerator)<\/li>\n<li><strong>Callbacks<\/strong> (popular in node)<\/li>\n<li><strong>Promises with <code>then()<\/code><\/strong> (ES2015+, available in older browsers if you use one of the many promise libraries)<\/li>\n<\/ul>\n<p><strong>All three are available in current browsers, and node 7+.<\/strong><\/p>\n<hr>\n<h2>ES2017+: Promises with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/async_function\"><code>async\/await<\/code><\/a><\/h2>\n<p>The ECMAScript version released in 2017 introduced <em>syntax-level support<\/em> for asynchronous functions. With the help of <code>async<\/code> and <code>await<\/code>, you can write asynchronous in a &#8220;synchronous style&#8221;. The code is still asynchronous, but it&#8217;s easier to read\/understand.<\/p>\n<p><code>async\/await<\/code> builds on top of promises: an <code>async<\/code> function always returns a promise. <code>await<\/code> &#8220;unwraps&#8221; a promise and either result in the value the promise was resolved with or throws an error if the promise was rejected.<\/p>\n<p><strong>Important:<\/strong> You can only use <code>await<\/code> inside an <code>async<\/code> function or in a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Modules\">JavaScript module<\/a>. Top-level <code>await<\/code> is not supported outside of modules, so you might have to make an async IIFE (<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Immediately_invoked_function_expression\">Immediately Invoked Function Expression<\/a>) to start an <code>async<\/code> context if not using a module.<\/p>\n<p>You can read more about <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/async_function\"><code>async<\/code><\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/await\"><code>await<\/code><\/a> on MDN.<\/p>\n<p>Here is an example that elaborates the <em>delay<\/em> function <code>findItem()<\/code> above:<\/p>\n<pre><code>\/\/ Using 'superagent' which will return a promise.\nvar superagent = require('superagent')\n\n\/\/ This is isn't declared as `async` because it already returns a promise\nfunction delay() {\n  \/\/ `delay` returns a promise\n  return new Promise(function(resolve, reject) {\n    \/\/ Only `delay` is able to resolve or reject the promise\n    setTimeout(function() {\n      resolve(42); \/\/ After 3 seconds, resolve the promise with value 42\n    }, 3000);\n  });\n}\n\nasync function getAllBooks() {\n  try {\n    \/\/ GET a list of book IDs of the current user\n    var bookIDs = await superagent.get('\/user\/books');\n    \/\/ wait for 3 seconds (just for the sake of this example)\n    await delay();\n    \/\/ GET information about each book\n    return superagent.get('\/books\/ids=\"+JSON.stringify(bookIDs));\n  } catch(error) {\n    \/\/ If any of the awaited promises was rejected, this catch block\n    \/\/ would catch the rejection reason\n    return null;\n  }\n}\n\n\/\/ Start an IIFE to use `await` at the top level\n(async function(){\n  let books = await getAllBooks();\n  console.log(books);\n})();\n<\/code><\/pre>\n<p>Current <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kangax.github.io\/compat-table\/es2016plus\/#test-async_functions\">browser<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/node.green\/#ES2017-features-async-functions\">node<\/a> versions support <code>async\/await<\/code>. You can also support older environments by transforming your code to ES5 with the help of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/facebook\/regenerator\">regenerator<\/a> (or tools that use regenerator, such as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/babeljs.io\/\">Babel<\/a>).<\/p>\n<hr>\n<h2>Let functions accept <em>callbacks<\/em><\/h2>\n<p>A callback is when function 1 is passed to function 2. Function 2 can call function 1 whenever it is ready. In the context of an asynchronous process, the callback will be called whenever the asynchronous process is done. Usually, the result is passed to the callback.<\/p>\n<p>In the example of the question, you can make <code>foo<\/code> accept a callback and use it as <code>success<\/code> callback. So this<\/p>\n<pre><code>var result = foo();\n\/\/ Code that depends on \"result'\n<\/code><\/pre>\n<p>becomes<\/p>\n<pre><code>foo(function(result) {\n    \/\/ Code that depends on 'result'\n});\n<\/code><\/pre>\n<p>Here we defined the function &#8220;inline&#8221; but you can pass any function reference:<\/p>\n<pre><code>function myCallback(result) {\n    \/\/ Code that depends on 'result'\n}\n\nfoo(myCallback);\n<\/code><\/pre>\n<p><code>foo<\/code> itself is defined as follows:<\/p>\n<pre><code>function foo(callback) {\n    $.ajax({\n        \/\/ ...\n        success: callback\n    });\n}\n<\/code><\/pre>\n<p><code>callback<\/code> will refer to the function we pass to <code>foo<\/code> when we call it and we pass it on to <code>success<\/code>. I.e. once the Ajax request is successful, <code>$.ajax<\/code> will call <code>callback<\/code> and pass the response to the callback (which can be referred to with <code>result<\/code>, since this is how we defined the callback).<\/p>\n<p>You can also process the response before passing it to the callback:<\/p>\n<pre><code>function foo(callback) {\n    $.ajax({\n        \/\/ ...\n        success: function(response) {\n            \/\/ For example, filter the response\n            callback(filtered_response);\n        }\n    });\n}\n<\/code><\/pre>\n<p>It&#8217;s easier to write code using callbacks than it may seem. After all, JavaScript in the browser is heavily event-driven (DOM events). Receiving the Ajax response is nothing else but an event.<br \/>\nDifficulties could arise when you have to work with third-party code, but most problems can be solved by just thinking through the application flow.<\/p>\n<hr>\n<h2>ES2015+: Promises with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise\">then()<\/a><\/h2>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise\">Promise API<\/a> is a new feature of ECMAScript 6 (ES2015), but it has good <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/caniuse.com\/#feat=promises\" title=\"caniuse\">browser support<\/a> already. There are also many libraries which implement the standard Promises API and provide additional methods to ease the use and composition of asynchronous functions (e.g., <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/petkaantonov\/bluebird\">bluebird<\/a>).<\/p>\n<p>Promises are containers for <em>future<\/em> values. When the promise receives the value (it is <em>resolved<\/em>) or when it is canceled (<em>rejected<\/em>), it notifies all of its &#8220;listeners&#8221; who want to access this value.<\/p>\n<p>The advantage over plain callbacks is that they allow you to decouple your code and they are easier to compose.<\/p>\n<p>Here is an example of using a promise:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>function delay() {\n  \/\/ `delay` returns a promise\n  return new Promise(function(resolve, reject) {\n    \/\/ Only `delay` is able to resolve or reject the promise\n    setTimeout(function() {\n      resolve(42); \/\/ After 3 seconds, resolve the promise with value 42\n    }, 3000);\n  });\n}\n\ndelay()\n  .then(function(v) { \/\/ `delay` returns a promise\n    console.log(v); \/\/ Log the value once it is resolved\n  })\n  .catch(function(v) {\n    \/\/ Or do something else if it is rejected\n    \/\/ (it would not happen in this example, since `reject` is not called).\n  });<\/code><\/pre>\n<pre class=\"snippet-code-css lang-css prettyprint-override\"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>Applied to our Ajax call we could use promises like this:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>function ajax(url) {\n  return new Promise(function(resolve, reject) {\n    var xhr = new XMLHttpRequest();\n    xhr.onload = function() {\n      resolve(this.responseText);\n    };\n    xhr.onerror = reject;\n    xhr.open('GET', url);\n    xhr.send();\n  });\n}\n\najax(\"https:\/\/jsonplaceholder.typicode.com\/todos\/1\")\n  .then(function(result) {\n    console.log(result); \/\/ Code depending on result\n  })\n  .catch(function() {\n    \/\/ An error occurred\n  });<\/code><\/pre>\n<pre class=\"snippet-code-css lang-css prettyprint-override\"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>Describing all the advantages that promise offer is beyond the scope of this answer, but if you write new code, you should seriously consider them. They provide a great abstraction and separation of your code.<\/p>\n<p>More information about promises: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.html5rocks.com\/en\/tutorials\/es6\/promises\/\">HTML5 rocks &#8211; JavaScript Promises<\/a>.<\/p>\n<h3>Side note: jQuery&#8217;s deferred objects<\/h3>\n<p>Deferred objects are jQuery&#8217;s custom implementation of promises (before the Promise API was standardized). They behave almost like promises but expose a slightly different API.<\/p>\n<p>Every Ajax method of jQuery already returns a &#8220;deferred object&#8221; (actually a promise of a deferred object) which you can just return from your function:<\/p>\n<pre><code>function ajax() {\n    return $.ajax(...);\n}\n\najax().done(function(result) {\n    \/\/ Code depending on result\n}).fail(function() {\n    \/\/ An error occurred\n});\n<\/code><\/pre>\n<h3>Side note: Promise gotchas<\/h3>\n<p>Keep in mind that promises and deferred objects are just <em>containers<\/em> for a future value, they are not the value itself. For example, suppose you had the following:<\/p>\n<pre><code>function checkPassword() {\n    return $.ajax({\n        url: '\/password',\n        data: {\n            username: $('#username').val(),\n            password: $('#password').val()\n        },\n        type: 'POST',\n        dataType: 'json'\n    });\n}\n\nif (checkPassword()) {\n    \/\/ Tell the user they're logged in\n}\n<\/code><\/pre>\n<p>This code misunderstands the above asynchronous issues. Specifically, <code>$.ajax()<\/code> doesn&#8217;t freeze the code while it checks the &#8216;\/password&#8217; page on your server &#8211; it sends a request to the server and while it waits, it immediately returns a jQuery Ajax Deferred object, not the response from the server. That means the <code>if<\/code> statement is going to always get this Deferred object, treat it as <code>true<\/code>, and proceed as though the user is logged in. Not good.<\/p>\n<p>But the fix is easy:<\/p>\n<pre><code>checkPassword()\n.done(function(r) {\n    if (r) {\n        \/\/ Tell the user they're logged in\n    } else {\n        \/\/ Tell the user their password was bad\n    }\n})\n.fail(function(x) {\n    \/\/ Tell the user something bad happened\n});\n<\/code><\/pre>\n<hr>\n<h2>Not recommended: Synchronous &#8220;Ajax&#8221; calls<\/h2>\n<p>As I mentioned, some(!) asynchronous operations have synchronous counterparts. I don&#8217;t advocate their use, but for completeness&#8217; sake, here is how you would perform a synchronous call:<\/p>\n<h3>Without jQuery<\/h3>\n<p>If you directly use a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/xhr.spec.whatwg.org\/\"><code>XMLHttpRequest<\/code><\/a> object, pass <code>false<\/code> as third argument to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/xhr.spec.whatwg.org\/#the-open()-method\"><code>.open<\/code><\/a>.<\/p>\n<h3>jQuery<\/h3>\n<p>If you use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.jquery.com\/jQuery.ajax\/\">jQuery<\/a>, you can set the <code>async<\/code> option to <code>false<\/code>. Note that this option is <em>deprecated<\/em> since jQuery 1.8.<br \/>\nYou can then either still use a <code>success<\/code> callback or access the <code>responseText<\/code> property of the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.jquery.com\/jQuery.ajax\/#jqXHR\">jqXHR object<\/a>:<\/p>\n<pre><code>function foo() {\n    var jqXHR = $.ajax({\n        \/\/...\n        async: false\n    });\n    return jqXHR.responseText;\n}\n<\/code><\/pre>\n<p>If you use any other jQuery Ajax method, such as <code>$.get<\/code>, <code>$.getJSON<\/code>, etc., you have to change it to <code>$.ajax<\/code> (since you can only pass configuration parameters to <code>$.ajax<\/code>).<\/p>\n<p><strong>Heads up!<\/strong> It is not possible to make a synchronous JSONP request. JSONP by its very nature is always asynchronous (one more reason to not even consider this option).<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">11<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How do I return the response from an asynchronous call? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] \u2192 For a more general explanation of asynchronous behaviour with different examples, see Why is my variable unaltered after I modify it inside of a function? &#8211; Asynchronous code reference \u2192 If you already understand the problem, skip to the possible solutions below. The problem The A in Ajax stands for asynchronous. That means &#8230; <a title=\"(Solved) How do I return the response from an asynchronous call?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\" aria-label=\"More on (Solved) How do I return the response from an asynchronous call?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[334,335,333],"class_list":["post-3829","post","type-post","status-publish","format-standard","hentry","category-solved","tag-ajax","tag-asynchronous","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) How do I return the response from an asynchronous call? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) How do I return the response from an asynchronous call? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] \u2192 For a more general explanation of asynchronous behaviour with different examples, see Why is my variable unaltered after I modify it inside of a function? &#8211; Asynchronous code reference \u2192 If you already understand the problem, skip to the possible solutions below. The problem The A in Ajax stands for asynchronous. That means ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T13:27:47+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) How do I return the response from an asynchronous call?\",\"datePublished\":\"2022-08-20T13:27:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\"},\"wordCount\":1351,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"ajax\",\"asynchronous\",\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\",\"name\":\"(Solved) How do I return the response from an asynchronous call? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-20T13:27:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) How do I return the response from an asynchronous call?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) How do I return the response from an asynchronous call? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) How do I return the response from an asynchronous call? - JassWeb","og_description":"[ad_1] \u2192 For a more general explanation of asynchronous behaviour with different examples, see Why is my variable unaltered after I modify it inside of a function? &#8211; Asynchronous code reference \u2192 If you already understand the problem, skip to the possible solutions below. The problem The A in Ajax stands for asynchronous. That means ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T13:27:47+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) How do I return the response from an asynchronous call?","datePublished":"2022-08-20T13:27:47+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/"},"wordCount":1351,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["ajax","asynchronous","javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/","url":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/","name":"(Solved) How do I return the response from an asynchronous call? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T13:27:47+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-return-the-response-from-an-asynchronous-call\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) How do I return the response from an asynchronous call?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3829"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3829\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}