{"id":198,"date":"2023-04-07T23:23:52","date_gmt":"2023-04-07T23:23:52","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=198"},"modified":"2023-08-23T16:22:28","modified_gmt":"2023-08-23T16:22:28","slug":"advanced-asynchronous-javascript-promises-async-await-fetch-api","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/","title":{"rendered":"Advanced Asynchronous JavaScript: Promises, Async\/Await, and Fetch API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Asynchronous programming is an essential part of modern web development, enabling seamless user experiences and improving application performance. Experienced developers often seek advanced techniques for handling asynchronous code to create more efficient, readable, and maintainable applications. In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Promises<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Promises are a powerful tool in JavaScript for handling asynchronous operations, providing a cleaner and more intuitive syntax compared to traditional callback-based patterns. They represent the eventual completion (or failure) of an asynchronous operation and its resulting value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 Creating and Consuming Promises<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To create a Promise, use the Promise constructor, which takes a single argument\u2014a function called the &#8220;executor.&#8221; The executor function receives two arguments: resolve and reject, which are functions used to fulfill or reject the Promise.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> myPromise = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Promise<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">resolve, reject<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-comment\">\/\/ Asynchronous operation<\/span>\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">To consume a Promise, use the <strong><code>.then()<\/code><\/strong> method to handle a fulfilled Promise and the <code><strong>.catch()<\/strong><\/code> method to handle a rejected Promise. Additionally, use the <code><strong>.finally()<\/strong><\/code> method to execute code regardless of whether the Promise is fulfilled or rejected.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">myPromise\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">result<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Fulfilled:'<\/span>, result);\n  })\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Rejected:'<\/span>, error);\n  })\n  .finally(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Promise settled'<\/span>);\n  });<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">1.2 Promise Chaining<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Promise chaining allows you to perform multiple asynchronous operations sequentially, with each <strong><code>.then()<\/code><\/strong> returning a new Promise.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">fetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>)\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.json())\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">data<\/span>) =&gt;<\/span> processData(data))\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">1.3 Promise Combinators<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Promise combinators like <strong><code>Promise.all()<\/code><\/strong>, <strong><code>Promise.race()<\/code><\/strong>, <strong><code>Promise.allSettled()<\/code><\/strong>, and <strong><code>Promise.any()<\/code><\/strong> can be used to handle multiple Promises simultaneously.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Async\/Await<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Async\/Await is a syntactic sugar built on top of Promises, providing a more readable and concise way to write asynchronous code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 The async keyword<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><code>async<\/code><\/strong> keyword is used to declare an asynchronous function, which always returns a Promise.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getData<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-comment\">\/\/ Asynchronous operation<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2.2 The await keyword<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><code>await<\/code><\/strong> keyword can only be used within an <strong><code>async<\/code><\/strong> function. It makes the function execution pause and wait for a Promise to resolve or reject before continuing.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getData<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> response = <span class=\"hljs-keyword\">await<\/span> fetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>);\n  <span class=\"hljs-keyword\">const<\/span> data = <span class=\"hljs-keyword\">await<\/span> response.json();\n  <span class=\"hljs-keyword\">return<\/span> data;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2.3 Error Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To handle errors with Async\/Await, use try-catch blocks.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getData<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n  <span class=\"hljs-keyword\">try<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> response = <span class=\"hljs-keyword\">await<\/span> fetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> data = <span class=\"hljs-keyword\">await<\/span> response.json();\n    <span class=\"hljs-keyword\">return<\/span> data;\n  } <span class=\"hljs-keyword\">catch<\/span> (error) {\n    <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error);\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">3. Fetch API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Fetch API is a modern, flexible, and powerful approach to making network requests in JavaScript, providing a more intuitive and efficient way to handle asynchronous data fetching compared to older methods like XMLHttpRequest.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 Basic Usage<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To make a simple GET request using the Fetch API, call the <code><strong>fetch()<\/strong><\/code> function with the URL as an argument.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">fetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>)\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.json())\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">data<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(data))\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3.2 Handling Different Response Types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Fetch API provides methods to handle different response types, such as .<code><strong>json()<\/strong><\/code>, .<strong><code>text()<\/code><\/strong>, .<strong><code>blob()<\/code><\/strong>, and .<strong><code>arrayBuffer()<\/code><\/strong>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">fetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>)\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.text())\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">text<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(text))\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3.3 Making POST Requests<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To make a POST request, pass an options object with <strong><code>method<\/code><\/strong>, <strong><code>headers<\/code><\/strong>, and <strong><code>body<\/code><\/strong> properties to the <strong><code>fetch()<\/code><\/strong> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">fetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>, {\n  <span class=\"hljs-attr\">method<\/span>: <span class=\"hljs-string\">'POST'<\/span>,\n  <span class=\"hljs-attr\">headers<\/span>: {\n    <span class=\"hljs-string\">'Content-Type'<\/span>: <span class=\"hljs-string\">'application\/json'<\/span>\n  },\n  <span class=\"hljs-attr\">body<\/span>: <span class=\"hljs-built_in\">JSON<\/span>.stringify({\n    <span class=\"hljs-attr\">key<\/span>: <span class=\"hljs-string\">'value'<\/span>\n  })\n})\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.json())\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">data<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(data))\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3.4 Handling Request and Response Headers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Fetch API provides the <strong><code>Headers<\/code><\/strong> class to manage request and response headers.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> headers = <span class=\"hljs-keyword\">new<\/span> Headers();\nheaders.append(<span class=\"hljs-string\">'Content-Type'<\/span>, <span class=\"hljs-string\">'application\/json'<\/span>);\nheaders.append(<span class=\"hljs-string\">'Authorization'<\/span>, <span class=\"hljs-string\">'Bearer '<\/span> + token);\n\nfetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/secure-data'<\/span>, {\n  <span class=\"hljs-attr\">method<\/span>: <span class=\"hljs-string\">'GET'<\/span>,\n  <span class=\"hljs-attr\">headers<\/span>: headers\n})\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.json())\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">data<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(data))\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3.5 AbortController<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><code>AbortController<\/code><\/strong> and <strong><code>AbortSignal<\/code><\/strong> classes can be used to cancel Fetch requests.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> controller = <span class=\"hljs-keyword\">new<\/span> AbortController();\n<span class=\"hljs-keyword\">const<\/span> signal = controller.signal;\n\nsetTimeout(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  controller.abort();\n}, <span class=\"hljs-number\">5000<\/span>);\n\nfetch(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>, { signal })\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.json())\n  .then(<span class=\"hljs-function\">(<span class=\"hljs-params\">data<\/span>) =&gt;<\/span> <span class=\"hljs-built_in\">console<\/span>.log(data))\n  .catch(<span class=\"hljs-function\">(<span class=\"hljs-params\">error<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">if<\/span> (error.name === <span class=\"hljs-string\">'AbortError'<\/span>) {\n      <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Fetch aborted'<\/span>);\n    } <span class=\"hljs-keyword\">else<\/span> {\n      <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error);\n    }\n  });<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Example Exercise<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this exercise, we will create a simple weather application that fetches weather data from an API, processes the data, and displays it on a web page. We will use advanced asynchronous JavaScript techniques, including Promises, Async\/Await, and the Fetch API.:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sign up for a free account on <a href=\"https:\/\/openweathermap.org\/api\" target=\"_blank\" rel=\"noreferrer noopener\">OpenWeather<\/a> to get an API key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create an <strong><code>index.html<\/code><\/strong> file with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"UTF-8\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>Weather App<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Weather App<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">form<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"weather-form\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"city\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\">\"Enter city name\"<\/span> <span class=\"hljs-attr\">required<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"submit\"<\/span>&gt;<\/span>Get Weather<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">form<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"weather-result\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"app.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Create an <strong><code>app.js<\/code><\/strong> file with the following content:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> API_KEY = <span class=\"hljs-string\">'your_api_key'<\/span>; <span class=\"hljs-comment\">\/\/ Replace with your OpenWeather API key<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> weatherForm = <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">'weather-form'<\/span>);\n<span class=\"hljs-keyword\">const<\/span> cityInput = <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">'city'<\/span>);\n<span class=\"hljs-keyword\">const<\/span> weatherResult = <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">'weather-result'<\/span>);\n\nweatherForm.addEventListener(<span class=\"hljs-string\">'submit'<\/span>, <span class=\"hljs-keyword\">async<\/span> (event) =&gt; {\n  event.preventDefault();\n  <span class=\"hljs-keyword\">const<\/span> city = cityInput.value.trim();\n\n  <span class=\"hljs-keyword\">try<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> weatherData = <span class=\"hljs-keyword\">await<\/span> fetchWeatherData(city);\n    <span class=\"hljs-keyword\">const<\/span> processedData = processWeatherData(weatherData);\n    displayWeather(processedData);\n  } <span class=\"hljs-keyword\">catch<\/span> (error) {\n    <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, error);\n    weatherResult.textContent = <span class=\"hljs-string\">'An error occurred while fetching weather data'<\/span>;\n  }\n});\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">fetchWeatherData<\/span>(<span class=\"hljs-params\">city<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> response = <span class=\"hljs-keyword\">await<\/span> fetch(\n    <span class=\"hljs-string\">`https:\/\/api.openweathermap.org\/data\/2.5\/weather?q=<span class=\"hljs-subst\">${city}<\/span>&amp;appid=<span class=\"hljs-subst\">${API_KEY}<\/span>&amp;units=metric`<\/span>\n  );\n\n  <span class=\"hljs-keyword\">if<\/span> (!response.ok) {\n    <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Error<\/span>(<span class=\"hljs-string\">`Failed to fetch weather data for <span class=\"hljs-subst\">${city}<\/span>`<\/span>);\n  }\n\n  <span class=\"hljs-keyword\">const<\/span> weatherData = <span class=\"hljs-keyword\">await<\/span> response.json();\n  <span class=\"hljs-keyword\">return<\/span> weatherData;\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">processWeatherData<\/span>(<span class=\"hljs-params\">weatherData<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> { name, sys, main, weather } = weatherData;\n  <span class=\"hljs-keyword\">const<\/span> { country } = sys;\n  <span class=\"hljs-keyword\">const<\/span> { temp, humidity } = main;\n  <span class=\"hljs-keyword\">const<\/span> { description } = weather&#91;<span class=\"hljs-number\">0<\/span>];\n\n  <span class=\"hljs-keyword\">return<\/span> {\n    <span class=\"hljs-attr\">city<\/span>: name,\n    <span class=\"hljs-attr\">country<\/span>: country,\n    <span class=\"hljs-attr\">temperature<\/span>: temp,\n    <span class=\"hljs-attr\">humidity<\/span>: humidity,\n    <span class=\"hljs-attr\">description<\/span>: description,\n  };\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">displayWeather<\/span>(<span class=\"hljs-params\">processedData<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">const<\/span> { city, country, temperature, humidity, description } = processedData;\n\n  weatherResult.innerHTML = <span class=\"hljs-string\">`\n    &lt;h2&gt;<span class=\"hljs-subst\">${city}<\/span>, <span class=\"hljs-subst\">${country}<\/span>&lt;\/h2&gt;\n    &lt;p&gt;Temperature: <span class=\"hljs-subst\">${temperature}<\/span>\u00b0C&lt;\/p&gt;\n    &lt;p&gt;Humidity: <span class=\"hljs-subst\">${humidity}<\/span>%&lt;\/p&gt;\n    &lt;p&gt;Weather: <span class=\"hljs-subst\">${description}<\/span>&lt;\/p&gt;\n  `<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Replace &#8216;<strong><code>your_api_key<\/code><\/strong>&#8216; in the <strong><code>app.js<\/code><\/strong> file with your OpenWeather API key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we have created a simple weather application that uses advanced asynchronous JavaScript techniques. The <strong><code>fetchWeatherData()<\/code><\/strong> function uses the Fetch API and Async\/Await to fetch weather data from the OpenWeather API. The <strong><code>processWeatherData()<\/code><\/strong> function processes the fetched data, and the <strong><code>displayWeather()<\/code><\/strong> function displays the processed data on the web page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have also used an event listener with an Async function to handle the form submission, making the code more readable and maintainable. The error handling is done using a try-catch block within the event listener, ensuring any errors that occur during the fetch or processing of data are caught and handled gracefully.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To test the application, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the <strong>index.html<\/strong> file in your favorite web browser.<\/li>\n\n\n\n<li>Enter a city name in the input field and click the &#8220;Get Weather&#8221; button.<\/li>\n\n\n\n<li>Observe the displayed weather data for the specified city, including temperature, humidity, and a description of the current weather.<\/li>\n\n\n\n<li>Try entering an invalid city name or disconnecting from the internet to see how the application handles errors.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This exercise demonstrates the power of advanced asynchronous JavaScript techniques, such as Promises, Async\/Await, and the Fetch API, in creating modern web applications that provide seamless user experiences and better performance. By using these techniques effectively, developers can write more efficient, readable, and maintainable code for handling asynchronous operations in their applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Asynchronous programming is an essential part of modern web development, enabling seamless user experiences and improving application performance. Experienced developers often seek advanced techniques for handling asynchronous code to create more efficient, readable, and maintainable applications. In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API. 1. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7,4],"tags":[],"class_list":["post-198","post","type-post","status-publish","format-standard","category-javascript","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Asynchronous JavaScript: Promises, Async\/Await, and Fetch API<\/title>\n<meta name=\"description\" content=\"In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Asynchronous JavaScript: Promises, Async\/Await, and Fetch API\" \/>\n<meta property=\"og:description\" content=\"In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-07T23:23:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-23T16:22:28+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Advanced Asynchronous JavaScript: Promises, Async\\\/Await, and Fetch API\",\"datePublished\":\"2023-04-07T23:23:52+00:00\",\"dateModified\":\"2023-08-23T16:22:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/\"},\"wordCount\":717,\"commentCount\":0,\"articleSection\":[\"JavaScript\",\"Programming Languages\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/\",\"name\":\"Asynchronous JavaScript: Promises, Async\\\/Await, and Fetch API\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2023-04-07T23:23:52+00:00\",\"dateModified\":\"2023-08-23T16:22:28+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\\\/Await, and the Fetch API\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/advanced-asynchronous-javascript-promises-async-await-fetch-api\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Advanced Asynchronous JavaScript: Promises, Async\\\/Await, and Fetch API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Asynchronous JavaScript: Promises, Async\/Await, and Fetch API","description":"In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API","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:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/","og_locale":"en_US","og_type":"article","og_title":"Asynchronous JavaScript: Promises, Async\/Await, and Fetch API","og_description":"In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API","og_url":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/","article_published_time":"2023-04-07T23:23:52+00:00","article_modified_time":"2023-08-23T16:22:28+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Advanced Asynchronous JavaScript: Promises, Async\/Await, and Fetch API","datePublished":"2023-04-07T23:23:52+00:00","dateModified":"2023-08-23T16:22:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/"},"wordCount":717,"commentCount":0,"articleSection":["JavaScript","Programming Languages"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/","url":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/","name":"Asynchronous JavaScript: Promises, Async\/Await, and Fetch API","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2023-04-07T23:23:52+00:00","dateModified":"2023-08-23T16:22:28+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"In this guide, we will explore advanced asynchronous JavaScript concepts, focusing on Promises, Async\/Await, and the Fetch API","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/advanced-asynchronous-javascript-promises-async-await-fetch-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"JavaScript","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/javascript\/"},{"@type":"ListItem","position":4,"name":"Advanced Asynchronous JavaScript: Promises, Async\/Await, and Fetch API"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=198"}],"version-history":[{"count":5,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":203,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/198\/revisions\/203"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}