{"id":676,"date":"2022-08-20T20:55:08","date_gmt":"2022-08-20T15:25:08","guid":{"rendered":"https:\/\/jassweb.com\/new22\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference\/"},"modified":"2022-08-20T20:55:08","modified_gmt":"2022-08-20T15:25:08","slug":"solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/","title":{"rendered":"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference"},"content":{"rendered":"<h2> Introduction <\/h2>\n<p>[ad_1]<\/p>\n<p>Asynchronous code is a type of programming that allows multiple tasks to be executed simultaneously. This type of code can be difficult to debug, as it can be difficult to determine why a variable is not being altered after it has been modified inside of a function. In this article, we will discuss the potential causes of this issue and how to solve it. We will also provide a reference to asynchronous code and explain how it works. By the end of this article, you should have a better understanding of why your variable is unaltered after you modify it inside of a function and how to fix it.<\/p>\n<h2> Solution<\/h2>\n<p><\/p>\n<p>The most likely cause of this issue is that the code inside the function is running asynchronously. This means that the code inside the function is running in the background, and the variable is not being updated until the asynchronous code has finished running.<\/p>\n<p>To fix this issue, you can use a callback function to ensure that the variable is updated after the asynchronous code has finished running. This callback function should be called after the asynchronous code has finished running, and it should update the variable with the new value. <\/p>\n<p><\/p>\n<div class=\"entry-content\" itemprop=\"text\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/p>\n<p><\/p>\n<div id=\"answer-23667087\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"23667087\" data-parentid=\"23667086\" data-score=\"694\" 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<p>One word answer: <strong>asynchronicity<\/strong>.<\/p>\n<h2>Forewords<\/h2>\n<p>This topic has been iterated at least a couple of thousands of times, here, in Stack Overflow. Hence, first off I\u2019d like to point out some extremely useful resources:<\/p>\n<ul>\n<li>\n<p>@Felix Kling\u2019s answer to \u201cHow do I return the response from an asynchronous call?\u201d. See his excellent answer explaining synchronous and asynchronous flows, as well as the \u201cRestructure code\u201d section.<br \/>\n@Benjamin Gruenbaum has also put a lot of effort explaining asynchronicity in the same thread.<\/p>\n<\/li>\n<li>\n<p>@Matt Esch\u2019s answer to \u201cGet data from fs.readFile\u201d also explains asynchronicity extremely well in a simple manner.<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2>The answer to the question at hand<\/h2>\n<p>Let\u2019s trace the common behavior first. In all examples, the <code>outerScopeVar<\/code> is modified inside of a <em>function<\/em>. That function is clearly not executed immediately, it is being assigned or passed as an argument. That is what we call a <strong><em>callback<\/em><\/strong>.<\/p>\n<p>Now the question is, when is that callback called?<\/p>\n<p>It depends on the case. Let\u2019s try to trace some common behavior again:<\/p>\n<ul>\n<li><code>img.onload<\/code> may be called <em>sometime in the future<\/em>, when (and if) the image has successfully loaded.<\/li>\n<li><code>setTimeout<\/code> may be called <em>sometime in the future<\/em>, after the delay has expired and the timeout hasn\u2019t been canceled by <code>clearTimeout<\/code>. Note: even when using <code>0<\/code> as delay, all browsers have a minimum timeout delay cap (specified to be 4ms in the HTML5 spec).<\/li>\n<li>jQuery <code>$.post<\/code>\u2018s callback may be called <em>sometime in the future<\/em>, when (and if) the Ajax request has been completed successfully.<\/li>\n<li>Node.js\u2019s <code>fs.readFile<\/code> may be called <em>sometime in the future<\/em>, when the file has been read successfully or thrown an error.<\/li>\n<\/ul>\n<p>In all cases, we have a callback which may run <em>sometime in the future<\/em>. This \u201csometime in the future\u201d is what we refer to as <strong>asynchronous flow<\/strong>.<\/p>\n<p>Asynchronous execution is pushed out of the synchronous flow. That is, the asynchronous code will <strong>never<\/strong> execute while the synchronous code stack is executing. This is the meaning of JavaScript being single-threaded.<\/p>\n<p>More specifically, when the JS engine is idle \u2014 not executing a stack of (a)synchronous code \u2014 it will poll for events that may have triggered asynchronous callbacks (e.g. expired timeout, received network response) and execute them one after another. This is regarded as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/EventLoop\">Event Loop<\/a>.<\/p>\n<p>That is, the asynchronous code highlighted in the hand-drawn red shapes may execute only after all the remaining synchronous code in their respective code blocks have executed:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png\" alt=\"async code highlighted\"><\/p>\n<p>In short, the callback functions are created synchronously but executed asynchronously. You just can\u2019t rely on the execution of an asynchronous function until you know it has executed, and how to do that?<\/p>\n<p>It is simple, really. The logic that depends on the asynchronous function execution should be started\/called from inside this asynchronous function. For example, moving the <code>alert<\/code>s and <code>console.log<\/code>s too inside the callback function would output the expected result, because the result is available at that point.<\/p>\n<h3>Implementing your own callback logic<\/h3>\n<p>Often you need to do more things with the result from an asynchronous function or do different things with the result depending on where the asynchronous function has been called. Let\u2019s tackle a bit more complex example:<\/p>\n<pre><code>var outerScopeVar;\nhelloCatAsync();\nalert(outerScopeVar);\n\nfunction helloCatAsync() {\n    setTimeout(function() {\n        outerScopeVar=\"Nya\";\n    }, Math.random() * 2000);\n}\n<\/code><\/pre>\n<p><strong>Note:<\/strong> I\u2019m using <code>setTimeout<\/code> with a random delay as a generic asynchronous function, the same example applies to Ajax, <code>readFile<\/code>, <code>onload<\/code> and any other asynchronous flow.<\/p>\n<p>This example clearly suffers from the same issue as the other examples, it is not waiting until the asynchronous function executes.<\/p>\n<p>Let\u2019s tackle it implementing a callback system of our own. First off, we get rid of that ugly <code>outerScopeVar<\/code> which is completely useless in this case. Then we add a parameter which accepts a function argument, our callback. When the asynchronous operation finishes, we call this callback passing the result. The implementation (please read the comments in order):<\/p>\n<pre><code>\/\/ 1. Call helloCatAsync passing a callback function,\n\/\/    which will be called receiving the result from the async operation\nhelloCatAsync(function(result) {\n    \/\/ 5. Received the result from the async function,\n    \/\/    now do whatever you want with it:\n    alert(result);\n});\n\n\/\/ 2. The \"callback\" parameter is a reference to the function which\n\/\/    was passed as argument from the helloCatAsync call\nfunction helloCatAsync(callback) {\n    \/\/ 3. Start async operation:\n    setTimeout(function() {\n        \/\/ 4. Finished async operation,\n        \/\/    call the callback passing the result as argument\n        callback('Nya');\n    }, Math.random() * 2000);\n}\n<\/code><\/pre>\n<p>Code snippet of the above example:<\/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>\/\/ 1. Call helloCatAsync passing a callback function,\n\/\/    which will be called receiving the result from the async operation\nconsole.log(\"1. function called...\")\nhelloCatAsync(function(result) {\n    \/\/ 5. Received the result from the async function,\n    \/\/    now do whatever you want with it:\n    console.log(\"5. result is: \", result);\n});\n\n\/\/ 2. The \"callback\" parameter is a reference to the function which\n\/\/    was passed as argument from the helloCatAsync call\nfunction helloCatAsync(callback) {\n    console.log(\"2. callback here is the function passed as argument above...\")\n    \/\/ 3. Start async operation:\n    setTimeout(function() {\n    console.log(\"3. start async operation...\")\n    console.log(\"4. finished async operation, calling the callback, passing the result...\")\n        \/\/ 4. Finished async operation,\n        \/\/    call the callback passing the result as argument\n        callback('Nya');\n    }, Math.random() * 2000);\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<p>Most often in real use cases, the DOM API and most libraries already provide the callback functionality (the <code>helloCatAsync<\/code> implementation in this demonstrative example). You only need to pass the callback function and understand that it will execute out of the synchronous flow, and restructure your code to accommodate for that.<\/p>\n<p>You will also notice that due to the asynchronous nature, it is impossible to <code>return<\/code> a value from an asynchronous flow back to the synchronous flow where the callback was defined, as the asynchronous callbacks are executed long after the synchronous code has already finished executing.<\/p>\n<p>Instead of <code>return<\/code>ing a value from an asynchronous callback, you will have to make use of the callback pattern, or\u2026 Promises.<\/p>\n<h3>Promises<\/h3>\n<p>Although there are ways to keep the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/callbackhell.com\/\">callback hell<\/a> at bay with vanilla JS, promises are growing in popularity and are currently being standardized in ES6 (see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise\">Promise \u2013 MDN<\/a>).<\/p>\n<p>Promises (a.k.a. Futures) provide a more linear, and thus pleasant, reading of the asynchronous code, but explaining their entire functionality is out of the scope of this question. Instead, I\u2019ll leave these excellent resources for the interested:<\/p>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.html5rocks.com\/en\/tutorials\/es6\/promises\/\">JavaScript Promises \u2013 HTML5 Rocks<\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/domenic.me\/2012\/10\/14\/youre-missing-the-point-of-promises\/\">You\u2019re Missing the Point of Promises \u2013 domenic.me<\/a><\/li>\n<\/ul>\n<hr>\n<h3>More reading material about JavaScript asynchronicity<\/h3>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/maxogden\/art-of-node#callbacks\">The Art of Node \u2013 Callbacks<\/a> explains asynchronous code and callbacks very well with vanilla JS examples and Node.js code as well.<\/li>\n<\/ul>\n<hr>\n<blockquote>\n<p><strong>Note:<\/strong> I\u2019ve marked this answer as Community Wiki, hence anyone with at least 100 reputations can edit and improve it! Please feel free to improve this answer, or submit a completely new answer if you\u2019d like as well.<\/p>\n<p>I want to turn this question into a canonical topic to answer asynchronicity issues which are unrelated to Ajax (there is How to return the response from an AJAX call? for that), hence this topic needs your help to be as good and helpful as possible!<\/p>\n<\/blockquote>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p> <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p>\n<\/div>\n<\/div>\n<p>solved Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference <\/p>\n<p><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/div>\n<p>[ad_2]<\/p>\n<p>When working with asynchronous code, it is important to understand why a variable may remain unaltered after it has been modified inside of a function. This is because asynchronous code is non-blocking, meaning that the code will not wait for the function to finish before continuing on to the next line of code. This means that the variable may not be updated until after the function has finished executing.<\/p>\n<p>In order to ensure that the variable is updated, it is important to use a callback function. A callback function is a function that is called after the asynchronous code has finished executing. This allows the variable to be updated before the next line of code is executed. It is also important to note that the callback function should be placed after the asynchronous code, as the variable may not be updated until after the callback function has been called.<\/p>\n<p>In summary, when working with asynchronous code, it is important to understand why a variable may remain unaltered after it has been modified inside of a function. This is because asynchronous code is non-blocking, meaning that the code will not wait for the function to finish before continuing on to the next line of code. To ensure that the variable is updated, it is important to use a callback function that is placed after the asynchronous code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] Asynchronous code is a type of programming that allows multiple tasks to be executed simultaneously. This type of code can be difficult to debug, as it can be difficult to determine why a variable is not being altered after it has been modified inside of a function. In this article, we will discuss &#8230; <a title=\"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\" aria-label=\"More on (Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[335,333],"class_list":["post-676","post","type-post","status-publish","format-standard","hentry","category-solved","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) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference - 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-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] Asynchronous code is a type of programming that allows multiple tasks to be executed simultaneously. This type of code can be difficult to debug, as it can be difficult to determine why a variable is not being altered after it has been modified inside of a function. In this article, we will discuss ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T15:25:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png\" \/>\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=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference\",\"datePublished\":\"2022-08-20T15:25:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\"},\"wordCount\":1409,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png\",\"keywords\":[\"asynchronous\",\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\",\"name\":\"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png\",\"datePublished\":\"2022-08-20T15:25:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference\"}]},{\"@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=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference - 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-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference - JassWeb","og_description":"Introduction [ad_1] Asynchronous code is a type of programming that allows multiple tasks to be executed simultaneously. This type of code can be difficult to debug, as it can be difficult to determine why a variable is not being altered after it has been modified inside of a function. In this article, we will discuss ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T15:25:08+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png","type":"","width":"","height":""}],"author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference","datePublished":"2022-08-20T15:25:08+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/"},"wordCount":1409,"commentCount":0,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png","keywords":["asynchronous","javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/","url":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/","name":"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png","datePublished":"2022-08-20T15:25:08+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-Why-is-my-variable-unaltered-after-I-modify-it.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchronous-code-reference-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) Why is my variable unaltered after I modify it inside of a function? \u2013 Asynchronous code reference"}]},{"@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=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","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\/676","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=676"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}