<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Async on Commentary of Takao</title><link>https://takao.blog/en/tags/async/</link><description>Recent content in Async on Commentary of Takao</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>Commentary of Takao</copyright><lastBuildDate>Sat, 13 Jun 2026 23:11:50 +0900</lastBuildDate><atom:link href="https://takao.blog/en/tags/async/index.xml" rel="self" type="application/rss+xml"/><item><title>Patterns to Avoid with JavaScript Async/Await</title><link>https://takao.blog/en/web/javascript-async-await-tips/</link><pubDate>Sat, 15 Feb 2025 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/javascript-async-await-tips/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Patterns to Avoid with JavaScript Async/Await" /&gt;&lt;h2 id="introduction"&gt;Introduction
&lt;/h2&gt;&lt;p&gt;Since the introduction of &lt;code&gt;async/await&lt;/code&gt; in ES2017, writing asynchronous JavaScript has become far more intuitive. It allows developers to express asynchronous logic in a synchronous-looking structure, drastically improving codebase readability compared to nested Promise chains (&lt;code&gt;.then().catch()&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;However, this clean synchronous-like syntax can hide performance pitfalls and error-handling bugs. In this article, we&amp;rsquo;ll dive into common async/await antipatterns and learn how to refactor them into high-performance, resilient code.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="antipattern-1-unnecessary-serialization-sequential-awaiting"&gt;Antipattern 1: Unnecessary Serialization (Sequential Awaiting)
&lt;/h2&gt;&lt;p&gt;The most common mistake is executing independent asynchronous tasks sequentially, missing opportunities for parallel execution.&lt;/p&gt;</description></item><item><title>JavaScript Event Loop Deep Dive: Microtasks, Macrotasks, and Beyond</title><link>https://takao.blog/en/web/js-event-loop/</link><pubDate>Mon, 19 Feb 2024 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/js-event-loop/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post JavaScript Event Loop Deep Dive: Microtasks, Macrotasks, and Beyond" /&gt;&lt;p&gt;The JavaScript event loop is one of the most important yet misunderstood concepts in the language. Despite JavaScript being single-threaded, its event loop enables non-blocking concurrency through a sophisticated queue system. This article builds a complete mental model of how JavaScript handles asynchronous execution, from the call stack to the microtask queue and everything in between.&lt;/p&gt;
&lt;h2 id="the-call-stack-and-execution-model"&gt;The Call Stack and Execution Model
&lt;/h2&gt;&lt;p&gt;JavaScript uses a call stack with a LIFO (Last In, First Out) structure. When a function is called, a new stack frame is pushed onto the stack. When the function returns, the frame is popped off. The key principle is run-to-completion: each function runs to completion before any other code can interrupt it.&lt;/p&gt;</description></item><item><title>Advanced JavaScript Promise Patterns: Async Control Flow Mastery</title><link>https://takao.blog/en/web/js-promise-patterns/</link><pubDate>Tue, 05 Dec 2023 00:00:00 +0900</pubDate><guid>https://takao.blog/en/web/js-promise-patterns/</guid><description>&lt;img src="https://takao.blog/img/thumnail.webp" alt="Featured image of post Advanced JavaScript Promise Patterns: Async Control Flow Mastery" /&gt;&lt;p&gt;Promises are fundamental to modern JavaScript, but mastering asynchronous control flow requires going beyond basic &lt;code&gt;.then()&lt;/code&gt; chains. This article explores advanced Promise patterns that intermediate to senior developers need for production applications, from concurrency control to error handling and cancellation.&lt;/p&gt;
&lt;h2 id="the-promise-landscape-beyond-basics"&gt;The Promise Landscape Beyond Basics
&lt;/h2&gt;&lt;p&gt;JavaScript provides four static methods on Promise for composing async operations, each suited to different scenarios:&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Method&lt;/th&gt;
					&lt;th&gt;Resolves When&lt;/th&gt;
					&lt;th&gt;Rejects When&lt;/th&gt;
					&lt;th&gt;Use Case&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Promise.all&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;All promises resolve&lt;/td&gt;
					&lt;td&gt;Any promise rejects&lt;/td&gt;
					&lt;td&gt;Parallel API calls, all required&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Promise.allSettled&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;All promises settle&lt;/td&gt;
					&lt;td&gt;Never&lt;/td&gt;
					&lt;td&gt;Mixed results, log all outcomes&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Promise.race&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;First promise settles&lt;/td&gt;
					&lt;td&gt;First promise rejects&lt;/td&gt;
					&lt;td&gt;Timeout race conditions&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;code&gt;Promise.any&lt;/code&gt;&lt;/td&gt;
					&lt;td&gt;First promise resolves&lt;/td&gt;
					&lt;td&gt;All promises reject&lt;/td&gt;
					&lt;td&gt;First-successful-response pattern&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;results&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;await&lt;/span&gt; Promise.&lt;span style="color:#a6e22e"&gt;allSettled&lt;/span&gt;([
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fetch&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/users&amp;#34;&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; =&amp;gt; &lt;span style="color:#a6e22e"&gt;r&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;()),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fetch&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/posts&amp;#34;&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; =&amp;gt; &lt;span style="color:#a6e22e"&gt;r&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;()),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fetch&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/api/comments&amp;#34;&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; =&amp;gt; &lt;span style="color:#a6e22e"&gt;r&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;()),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;successful&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;results&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;filter&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; =&amp;gt; &lt;span style="color:#a6e22e"&gt;r&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;status&lt;/span&gt; &lt;span style="color:#f92672"&gt;===&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fulfilled&amp;#34;&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;map&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; =&amp;gt; &lt;span style="color:#a6e22e"&gt;r&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Promise.allSettled&lt;/code&gt; is particularly useful when you need to process partial results even if some operations fail, such as batch data synchronization where individual record failures should not abort the entire batch.&lt;/p&gt;</description></item></channel></rss>