<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhisek Goswami</title>
    <description>The latest articles on DEV Community by Abhisek Goswami (@abhisekgoswami).</description>
    <link>https://dev.to/abhisekgoswami</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4281044%2F027cf615-1c23-4bcd-aa95-2786040f103b.jpg</url>
      <title>DEV Community: Abhisek Goswami</title>
      <link>https://dev.to/abhisekgoswami</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhisekgoswami"/>
    <language>en</language>
    <item>
      <title>JavaScript asynchronous programming: callbacks, promises and async/await</title>
      <dc:creator>Abhisek Goswami</dc:creator>
      <pubDate>Thu, 03 Dec 2026 03:30:00 +0000</pubDate>
      <link>https://dev.to/abhisekgoswami/javascript-asynchronous-programming-callbacks-promises-and-asyncawait-jm2</link>
      <guid>https://dev.to/abhisekgoswami/javascript-asynchronous-programming-callbacks-promises-and-asyncawait-jm2</guid>
      <description>&lt;p&gt;JavaScript started in the 1990s as a way to add a little life to static web pages. Today it runs interactive sites, single-page apps, mobile apps through frameworks like React, and servers through Node.js.&lt;/p&gt;

&lt;p&gt;One feature makes all of that possible: &lt;strong&gt;asynchronous programming&lt;/strong&gt;. This guide explains what it is, the three main ways to write it, and how they relate, with a small example you can run.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is asynchronous programming?
&lt;/h2&gt;

&lt;p&gt;Traditional code runs step by step: start a task, wait for it to finish, then move on. Asynchronous code starts a task and &lt;strong&gt;keeps going&lt;/strong&gt; while it runs in the background. When the result arrives, the program picks it up.&lt;/p&gt;

&lt;p&gt;That brings three benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A better user experience.&lt;/strong&gt; The page stays responsive while work happens in the background.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better performance.&lt;/strong&gt; Waiting time (for a network call, a file, a timer) is used for other work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability.&lt;/strong&gt; Servers and real-time apps can handle many users at once.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A practical example
&lt;/h3&gt;

&lt;p&gt;Picture a weather app. The old way: fetch the data, wait, then update the screen, with a frozen page in between. The asynchronous way: request the data, keep showing what you already have, and update the screen the moment fresh data arrives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four approaches in JavaScript
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;Restaurant analogy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Callbacks&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A function passed to another function, run when a task finishes&lt;/td&gt;
&lt;td&gt;The waiter brings your food when it's ready&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Promises&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;An object that stands for a future result (or failure)&lt;/td&gt;
&lt;td&gt;A receipt that guarantees your meal, even if it takes a while&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;async/await&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Syntax on top of promises that reads like normal code&lt;/td&gt;
&lt;td&gt;You read a magazine and pick up where you were when the food arrives&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Web Workers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Separate threads for heavy work&lt;/td&gt;
&lt;td&gt;Assistant chefs in a second kitchen&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Which one to use depends on how complex the work is, how readable the code must be, and whether the job is CPU-heavy (Web Workers) or mostly waiting (the other three). Every modern browser supports async/await, so it's the usual choice today.&lt;/p&gt;

&lt;p&gt;The rest of this guide covers callbacks, promises and async/await.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks
&lt;/h2&gt;

&lt;p&gt;A callback is a function you pass to another function so it can be run later.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A function that does something asynchronous accepts a callback as an argument.&lt;/li&gt;
&lt;li&gt;It starts the operation (fetching data, waiting for a click).&lt;/li&gt;
&lt;li&gt;When the operation finishes, it calls your callback with the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Callbacks are simple, but they nest badly. Three or four dependent steps quickly turn into the "pyramid" known as &lt;strong&gt;callback hell&lt;/strong&gt;. Promises were introduced to fix that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;A promise is an object representing the eventual result of an asynchronous operation. It's always in one of three states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pending&lt;/strong&gt; : the operation is still running.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fulfilled&lt;/strong&gt; : it succeeded, and the promise holds the result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rejected&lt;/strong&gt; : it failed, and the promise holds the error.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You react to the outcome with &lt;code&gt;.then()&lt;/code&gt; for success and &lt;code&gt;.catch()&lt;/code&gt; for errors. Compared with callbacks, promises give you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Readability:&lt;/strong&gt; flat chains instead of nested pyramids.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier error handling:&lt;/strong&gt; one &lt;code&gt;.catch()&lt;/code&gt; can handle errors from the whole chain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sequencing:&lt;/strong&gt; chained steps run in a clear order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Promises have drawbacks too. Long chains can still be hard to follow ("promise hell"), and the extra layer takes some getting used to. That led to async/await.&lt;/p&gt;

&lt;h2&gt;
  
  
  async/await
&lt;/h2&gt;

&lt;p&gt;Introduced in ECMAScript 2017, &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; are a cleaner way to write promise-based code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; marks a function as asynchronous. It always returns a promise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await&lt;/code&gt; pauses &lt;strong&gt;that function&lt;/strong&gt; until a promise settles, then gives you its value. The rest of the program keeps running.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result is code that reads top to bottom, like the steps you would describe out loud, with ordinary &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; for errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  How async/await relates to promises
&lt;/h3&gt;

&lt;p&gt;async/await doesn't replace promises; it's built on them.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The request:&lt;/strong&gt; an &lt;code&gt;async&lt;/code&gt; function starts an operation that returns a promise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The wait:&lt;/strong&gt; &lt;code&gt;await&lt;/code&gt; pauses the function, not the whole program.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The resolution:&lt;/strong&gt; the promise fulfils or rejects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The continuation:&lt;/strong&gt; the function resumes with the value, or jumps to &lt;code&gt;catch&lt;/code&gt; with the error.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A worked example: square roots
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. A promise-based function&lt;/strong&gt; (with a one-second delay to simulate slow work):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateSquareRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Cannot calculate the square root of a negative number"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Using it with async/await:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateSquareRootAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;calculateSquareRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Square root:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Error:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;calculateSquareRootAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Square root: 3&lt;/span&gt;
&lt;span class="nf"&gt;calculateSquareRootAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Cannot calculate the square root of a negative number&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What's happening:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;calculateSquareRoot&lt;/code&gt; creates a promise. Its executor resolves with the square root, or rejects for negative numbers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;calculateSquareRootAsync&lt;/code&gt; is marked &lt;code&gt;async&lt;/code&gt;, so it can use &lt;code&gt;await&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await&lt;/code&gt; pauses the function until the promise settles. On success the value is logged; on failure, &lt;code&gt;catch&lt;/code&gt; handles the error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Promises are the foundation; async/await is friendlier syntax on top.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; functions always return promises.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await&lt;/code&gt; pauses only the current function until the promise settles.&lt;/li&gt;
&lt;li&gt;Understanding promises makes async/await much easier to use well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Further reading:&lt;/strong&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Async_JS" rel="noopener noreferrer"&gt;Asynchronous JavaScript on MDN&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;Promise reference&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function" rel="noopener noreferrer"&gt;async function reference&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Building an API next? Read &lt;a href="https://abhisek-goswami.com/blog/rest-apis-explained/" rel="noopener noreferrer"&gt;REST APIs explained with simple examples&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abhisek-goswami.com/blog/javascript-async-callbacks-promises-async-await/" rel="noopener noreferrer"&gt;abhisek-goswami.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
