Skip to Content
JavaScriptデザインパターンを学ぶ 第2版
book

JavaScriptデザインパターンを学ぶ 第2版

by Addy Osmani
March 2025
Intermediate to advanced
298 pages
4h 18m
Japanese
O'Reilly Media, Inc.
Content preview from JavaScriptデザインパターンを学ぶ 第2版

第9章. 非同期プログラミングパターン

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

非同期JavaScriptプログラミング では、バックグラウンドで長時間実行するタスクを実行しながら、ブラウザがイベントに応答し、そのイベントを処理するために他のコードを実行することができる。非同期プログラミングはJavaScriptでは比較的新しいもので、本書の初版が出版されたときには、それをサポートする構文はなかった。

promiseasyncawait といったJavaScriptの概念は、メインスレッドをブロックすることなく、コードをすっきりさせ、読み取りやすくする。async 関数は2016年にES7の一部として導入され、現在ではすべてのブラウザでサポートされている。これらの機能を使ってアプリケーション・フローを構成するパターンをいくつか見てみよう。

非同期プログラミング

JavaScriptでは、同期コードは 、ブロッキング方式で実行される。つまり、コードは1ステートメントずつシリアルに実行される。次のコードは、現在の文の実行が完了した後にのみ実行できる。同期関数を呼び出すと、制御が呼び出し元に戻る前に、その関数内のコードが最初から最後まで実行される。

一方、非同期コードは非ブロッキングで実行される。つまり、JavaScriptエンジンは、現在実行中のコードが何かを待っている間に、バックグラウンドでこのコードの実行に切り替えることができる。非同期関数を呼び出すと、関数内のコードはバックグラウンドで実行され、制御はすぐに呼び出し元に戻る。

JavaScriptの同期コードの例である:

function synchronousFunction() {
  // do something
}

synchronousFunction();
// the code inside the function is executed before this line

そして、ここにJavaScriptの非同期コードの例を示す:

function asynchronousFunction() {
  // do something
}

asynchronousFunction();
// the code inside the function is executed in the background
// while control returns to this line

一般的に、非同期コードを使えば、他のコードをブロックすることなく、長時間実行する演算子を実行できる。非同期コードは、ネットワークへのリクエスト、データベースへの読み取りや書き込み、その他のタイプのI/O(入出力)操作を行うときに適している。

asyncawaitpromise のような のような言語機能は、JavaScriptで非同期コードを書くことを容易にする。これらの機能により、非同期コードを同期コードと同じように記述することができ、読みやすく理解しやすくなる。

コールバック、プロミス、async/await 、それぞれの違いを簡単に見てから、さらに深く掘り下げていこう:

// using callbacks
function makeRequest(url, callback) {
  fetch(url)
    .then(response => response.json())
    .then(
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

アルゴリズムクイックリファレンス 第2版

アルゴリズムクイックリファレンス 第2版

George T. Heineman, Gary Pollice, Stanley Selkow, 黒川 利明, 黒川 洋
Node.jsデザインパターン 第2版

Node.jsデザインパターン 第2版

Mario Casciaro, Luciano Mammino, 武舎 広幸, 阿部 和也

Publisher Resources

ISBN: 9798341625952