Searching...
Tiếng Việt
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Async JavaScript

Async JavaScript

bởi Trevor Burnham 2012 104 trang
4.00
204 đánh giá
Nghe
Try Full Access for 7 Days
Unlock listening & more!
Continue

Điểm chính

1. Kiến trúc sự kiện của JavaScript giúp ứng dụng phản hồi nhanh, không bị chặn

Mã JavaScript không bao giờ bị gián đoạn, vì sự kiện chỉ được xếp hàng khi mã đang chạy; chúng không thể kích hoạt cho đến khi mã hoàn thành.

Thiết kế đơn luồng. Vòng lặp sự kiện của JavaScript cho phép xử lý nhiều tác vụ mà không làm tắc nghẽn. Khi một thao tác bất đồng bộ được khởi tạo, JavaScript tiếp tục chạy các đoạn mã khác trong khi chờ thao tác đó hoàn tất. Điều này giúp giao diện người dùng phản hồi nhanh và xử lý I/O hiệu quả.

Sự kiện như những viên gạch xây dựng. Các hàm bất đồng bộ trong JavaScript thường thuộc hai loại: I/O và thời gian. Những hàm này xếp hàng các sự kiện để xử lý khi ngăn xếp thực thi hiện tại trống. Mô hình này cho phép lập trình viên viết mã không chặn, xử lý nhiều tác vụ đồng thời mà không cần phức tạp của đa luồng truyền thống.

Lập trình dựa trên callback. Thay vì chờ thao tác hoàn thành, JavaScript dùng callback để định nghĩa hành động khi thao tác kết thúc. Cách tiếp cận này đòi hỏi tư duy khác so với lập trình đồng bộ, nhưng giúp sử dụng tài nguyên hiệu quả hơn, đặc biệt trong các ứng dụng nặng I/O như máy chủ web hay giao diện người dùng.

2. Mẫu PubSub phân phối sự kiện giúp mã sạch và mô-đun hơn

PubSub giúp dễ dàng đặt tên, phân phối và xếp chồng sự kiện. Bất cứ khi nào một đối tượng cần thông báo điều gì đó đã xảy ra, PubSub là mẫu thiết kế tuyệt vời để dùng.

Tách rời các thành phần. Mẫu Publish/Subscribe (PubSub) cho phép các phần khác nhau của ứng dụng giao tiếp mà không phụ thuộc trực tiếp. Người phát sự kiện phát ra sự kiện mà không biết ai nhận, người đăng ký lắng nghe sự kiện mà không biết ai phát. Điều này tạo ra mã mô-đun và dễ bảo trì hơn.

Linh hoạt và mở rộng. PubSub cho phép thêm hoặc loại bỏ bộ xử lý sự kiện dễ dàng mà không cần sửa mã phát sự kiện. Điều này giúp mở rộng chức năng hoặc gỡ lỗi các hành vi cụ thể đơn giản hơn. Nhiều framework và thư viện hiện đại, như jQuery hay EventEmitter của Node.js, đều áp dụng mẫu PubSub tương tự.

Phân phối sự kiện. PubSub đặc biệt hữu ích để phân phối sự kiện trong toàn ứng dụng. Ví dụ, một hành động của người dùng có thể kích hoạt cập nhật nhiều thành phần giao diện, thay đổi mô hình dữ liệu và giao tiếp với máy chủ. PubSub cho phép định nghĩa các phản hồi này độc lập, giúp mã dễ hiểu và bảo trì.

3. Promises đơn giản hóa thao tác bất đồng bộ và xử lý lỗi

Promises là tập hợp những việc bạn muốn xảy ra khi một quá trình kết thúc.

Biểu diễn giá trị tương lai. Promises cung cấp cách chuẩn để xử lý thao tác bất đồng bộ. Chúng đại diện cho một giá trị có thể chưa có ngay lập tức nhưng sẽ được giải quyết trong tương lai. Trừu tượng này giúp mã sạch hơn, trực quan hơn khi làm việc với tác vụ bất đồng bộ.

Chuỗi và kết hợp. Promises có thể được xâu chuỗi, cho phép biểu diễn rõ ràng các chuỗi thao tác bất đồng bộ phức tạp. Các phương thức như .then() và .catch() giúp xử lý kết quả thành công và lỗi một cách có cấu trúc. Điều này loại bỏ “địa ngục callback” thường gặp trong mã bất đồng bộ lồng nhau sâu.

Truyền lỗi. Promises tự động truyền lỗi qua chuỗi thao tác, giúp xử lý ngoại lệ trong mã bất đồng bộ dễ dàng hơn. Đây là cải tiến lớn so với cách dùng callback truyền thống, nơi xử lý lỗi thường phải kiểm tra lặp đi lặp lại ở mỗi bước.

4. Thư viện điều khiển luồng như Async.js giúp quản lý quy trình bất đồng bộ phức tạp

Nếu bạn gặp vấn đề về điều khiển luồng, rất có thể Async.js đã có giải pháp.

Quản lý luồng phức tạp. Các thư viện như Async.js cung cấp trừu tượng cấp cao cho các mẫu bất đồng bộ phổ biến. Chúng có các phương thức để chạy tác vụ theo chuỗi, song song hoặc kết hợp phức tạp. Điều này giúp triển khai quy trình làm việc khó quản lý bằng callback thô hoặc Promises trở nên đơn giản hơn.

Mẫu chuẩn hóa. Thư viện điều khiển luồng chuẩn hóa các mẫu async phổ biến, làm mã dễ đọc và bảo trì. Ví dụ, Async.js có các phương thức như .map(), .filter(), .reduce() hoạt động với thao tác bất đồng bộ, tương tự các phương thức mảng quen thuộc nhưng dành cho luồng async.

Hiệu năng và kiểm soát đồng thời. Những thư viện này thường cho phép kiểm soát chi tiết số lượng tác vụ chạy đồng thời. Điều này rất quan trọng để tối ưu hiệu năng, nhất là khi xử lý tác vụ I/O hoặc API giới hạn tốc độ.

5. Web Workers và Node.js clusters cho phép xử lý song song thực sự

Tính toán phân tán chưa bao giờ thú vị đến thế.

Tận dụng đa lõi CPU. Web Workers trên trình duyệt và module cluster trong Node.js cho phép ứng dụng JavaScript sử dụng nhiều lõi CPU. Điều này tạo ra xử lý song song thực sự, cần thiết cho các tác vụ tính toán nặng mà nếu không sẽ làm tắc nghẽn luồng chính.

Môi trường thực thi cô lập. Worker chạy trong môi trường riêng biệt, không chia sẻ trạng thái. Điều này loại bỏ nhiều phức tạp của đa luồng truyền thống như điều kiện tranh chấp hay deadlock. Giao tiếp giữa worker và luồng chính được thực hiện qua giao diện truyền tin.

Khả năng mở rộng cho ứng dụng phía máy chủ. Trong Node.js, module cluster cho phép một máy chủ xử lý nhiều kết nối đồng thời hơn bằng cách tạo các tiến trình worker. Điều này rất hữu ích để mở rộng máy chủ web tận dụng hệ thống đa lõi mà không cần cấu hình cân bằng tải phức tạp.

6. Tải script bất đồng bộ tối ưu thời gian tải trang

Tối ưu tải trang là chủ đề rộng lớn, trong đó tải script chỉ là một yếu tố.

Cân bằng tốc độ và chức năng. Tải script bất đồng bộ giúp trang web tải nhanh hơn bằng cách hoãn tải và thực thi các script không quan trọng. Điều này cải thiện cảm nhận hiệu năng của ứng dụng web, cho phép nội dung hiển thị nhanh trong khi script tải ngầm.

Thuộc tính tải của HTML5. Các thuộc tính async và defer cung cấp cách tải script bất đồng bộ tích hợp trình duyệt:

  • defer: Tải script trong khi phân tích cú pháp tiếp tục, nhưng hoãn thực thi đến khi phân tích xong
  • async: Tải và thực thi script ngay khi có sẵn, không chờ phân tích cú pháp

Bộ tải script và hệ thống module. Kỹ thuật nâng cao dùng bộ tải script hoặc hệ thống module JavaScript (như RequireJS) cho phép kiểm soát tải script tốt hơn, quản lý phụ thuộc và tải có điều kiện theo nhu cầu ứng dụng.

7. Thành thạo các mẫu bất đồng bộ là chìa khóa viết JavaScript hiệu quả

Xử lý tập hợp sự kiện phức tạp một cách tinh tế vẫn là lĩnh vực mới mẻ trong JavaScript.

Thực hành tốt ngày càng phát triển. Khi ứng dụng JavaScript ngày càng phức tạp, việc thành thạo các mẫu bất đồng bộ trở nên quan trọng hơn bao giờ hết. Hiểu và sử dụng hiệu quả callback, Promises, async/await và các mẫu async khác là điều cần thiết để viết mã JavaScript hiệu quả, có khả năng mở rộng.

Cân bằng trừu tượng và hiệu năng. Mặc dù các trừu tượng cao như Promises và async/await giúp mã bất đồng bộ dễ viết và hiểu hơn, việc hiểu cơ chế bên dưới là rất quan trọng. Kiến thức này giúp lập trình viên chọn lựa mẫu phù hợp trong từng tình huống, cân bằng giữa rõ ràng mã và yêu cầu hiệu năng.

Học hỏi liên tục. Hệ sinh thái JavaScript luôn phát triển, với các mẫu và thực hành tốt mới liên tục xuất hiện. Cập nhật kiến thức và biết khi nào, cách nào áp dụng chúng là điều thiết yếu cho lập trình viên JavaScript muốn viết ứng dụng tiên tiến, hiệu quả.

Cập nhật lần cuối:

Want to read the full book?

FAQ

What's "Async JavaScript" by Trevor Burnham about?

  • Focus on Asynchronous JavaScript: The book is dedicated to understanding and mastering asynchronous JavaScript, which is crucial for building responsive web applications.
  • Concurrency and Event Handling: It explores how to handle concurrency and asynchronous tasks effectively without getting overwhelmed by complexity.
  • Practical Examples: The book provides practical examples and code snippets to illustrate how to implement asynchronous patterns in both client-side and server-side JavaScript.
  • Comprehensive Guide: It serves as a comprehensive guide for intermediate JavaScript developers looking to deepen their understanding of asynchronous programming.

Why should I read "Async JavaScript" by Trevor Burnham?

  • Improve Responsiveness: Learn how to build more responsive applications by effectively managing asynchronous operations.
  • Avoid Common Pitfalls: The book helps you avoid common pitfalls associated with asynchronous JavaScript, such as callback hell and race conditions.
  • Enhance JavaScript Skills: It is an excellent resource for intermediate developers to enhance their JavaScript skills, particularly in handling asynchronous tasks.
  • Practical Insights: Gain practical insights and techniques that can be directly applied to real-world projects, improving both code quality and performance.

What are the key takeaways of "Async JavaScript" by Trevor Burnham?

  • Understanding Events: A deep dive into JavaScript's event model and how asynchronous events are scheduled and handled.
  • Promises and Deferreds: Learn about Promises and Deferreds, which are essential for managing asynchronous operations in a more structured way.
  • Flow Control Libraries: Introduction to libraries like Async.js and Step, which help manage complex asynchronous flows.
  • Multithreading with Workers: Explore how to use web workers and Node.js workers to leverage multithreading in JavaScript applications.

How does "Async JavaScript" explain JavaScript events?

  • Conceptual Elegance: The book explains that JavaScript events are conceptually elegant and practical, allowing for uninterruptible code execution.
  • Event Scheduling: It covers how events are scheduled and executed in JavaScript, emphasizing the single-threaded nature of the language.
  • Event Queue: The book describes the event queue mechanism, where events are queued and executed sequentially once the current execution stack is clear.
  • Practical Examples: Provides practical examples to illustrate how to handle events and avoid common issues like the "Pyramid of Doom."

What are Promises and Deferreds in "Async JavaScript"?

  • Promises Defined: Promises are objects representing a task with two possible outcomes: success or failure, allowing for structured handling of asynchronous operations.
  • Deferreds as Supersets: A Deferred is a superset of a Promise, providing additional methods to trigger the Promise's state changes.
  • Encapsulation Benefits: Promises allow for better encapsulation of asynchronous tasks, making it easier to manage multiple callbacks and reduce code duplication.
  • Combining Promises: The book explains how to combine Promises to manage complex asynchronous flows, such as waiting for multiple tasks to complete.

How does "Async JavaScript" address flow control with Async.js?

  • Async.js Overview: Async.js is introduced as a powerful library for managing asynchronous flows, particularly in Node.js environments.
  • Collection Methods: The book covers Async.js's collection methods like forEach, map, and filter, which simplify handling arrays of asynchronous tasks.
  • Task Organization: It explains how to organize tasks using methods like series, parallel, and queue to control the execution order and concurrency.
  • Error Handling: Async.js's approach to error handling is discussed, showing how errors are propagated through callbacks to a final handler.

What is the role of web workers in "Async JavaScript"?

  • Multithreading Solution: Web workers are presented as a solution for leveraging multithreading in JavaScript without sharing state between threads.
  • Separate Execution: Workers run code in separate threads, allowing for complex computations without blocking the main thread.
  • Message Passing: The book explains how to communicate with workers using message passing, maintaining the event-driven nature of JavaScript.
  • Practical Applications: Examples of practical applications for web workers, such as video decoding and data parsing, are provided.

How does "Async JavaScript" suggest handling async script loading?

  • Script Loading Challenges: The book addresses the challenges of script loading, such as blocking and nonblocking scripts, and their impact on page performance.
  • HTML5 Attributes: It introduces HTML5 attributes like async and defer to optimize script loading and execution order.
  • Script Loading Libraries: Libraries like yepnope and Require.js are discussed for more advanced script loading scenarios, including conditional and modular loading.
  • Performance Optimization: Emphasizes the importance of optimizing script loading to improve page load times and user experience.

What are the best quotes from "Async JavaScript" and what do they mean?

  • "JavaScript now works." This quote highlights the evolution of JavaScript from a language often criticized for its limitations to a powerful tool for web development.
  • "Any application that can be written in JavaScript will eventually be written in JavaScript." Known as Atwood's Law, this quote underscores JavaScript's ubiquity and versatility across different platforms and applications.
  • "I love async, but I can’t code like this." This quote reflects the common frustration with callback hell and the need for better async management techniques.
  • "Let’s prove to the world that even the most complex problems can be tackled with clean, maintainable JavaScript code." This quote encapsulates the book's mission to empower developers to write efficient and maintainable asynchronous JavaScript.

How does "Async JavaScript" explain the use of PubSub?

  • PubSub Pattern: The book introduces the publish/subscribe pattern (PubSub) as a way to distribute events across an application.
  • EventEmitter in Node.js: It explains how Node.js's EventEmitter provides a built-in PubSub mechanism for handling events.
  • Evented Models: Discusses the use of evented models in frameworks like Backbone.js to manage application state changes and trigger events.
  • Custom jQuery Events: The book also covers how to use custom jQuery events for distributing DOM-related events efficiently.

What are the practical applications of "Async JavaScript" concepts?

  • Responsive Web Apps: The concepts help in building more responsive web applications by efficiently managing asynchronous tasks.
  • Server-Side JavaScript: Techniques like Promises and Async.js are applicable in server-side JavaScript environments, such as Node.js, for handling I/O operations.
  • Complex Event Handling: The book provides strategies for handling complex event-driven architectures, making it easier to manage large-scale applications.
  • Performance Optimization: By understanding and applying async patterns, developers can optimize performance and improve user experience in web applications.

What resources does "Async JavaScript" recommend for further learning?

  • JavaScript Weekly: The book recommends JavaScript Weekly for staying updated with the latest JavaScript news and trends.
  • Codecademy and CodeSchool: These platforms are suggested for interactive tutorials and courses to learn JavaScript and related technologies.
  • Eloquent JavaScript: A formal introduction to JavaScript is recommended through the book "Eloquent JavaScript" by Marijn Haverbeke.
  • Mozilla Developer Network: For reliable JavaScript documentation and resources, the book advises using the Mozilla Developer Network (MDN).

Đánh giá

4.00 trên tổng số 5
Trung bình của 204 đánh giá từ GoodreadsAmazon.

Cuốn sách Async JavaScript nhận được nhiều đánh giá tích cực, khi độc giả khen ngợi cách tiếp cận tập trung vào lập trình bất đồng bộ. Nhiều người cho rằng nội dung rất bổ ích và mở rộng tầm hiểu biết, đặc biệt là về hàng đợi sự kiện, promises và web workers. Độc giả đánh giá cao sự cô đọng của cuốn sách cùng khả năng giúp họ hiểu rõ hơn về mô hình đơn luồng của JavaScript. Dù có một số ý kiến cho rằng sách hơi lỗi thời hoặc thiếu ví dụ sâu sắc, nhưng nhìn chung, đây vẫn là lựa chọn đáng giá cho những ai muốn nâng cao kỹ năng JavaScript, nhất là trong việc xử lý các thao tác bất đồng bộ và tạo ra ứng dụng phản hồi nhanh hơn.

Your rating:
4.44
36 đánh giá

Về tác giả

Trevor Burnham là tác giả của cuốn sách "Async JavaScript," một tác phẩm đã thu hút sự chú ý lớn trong cộng đồng lập trình. Công trình của Burnham tập trung vào việc giải quyết một trong những khía cạnh khó nhằn nhất của lập trình JavaScript: tính bất đồng bộ mà không có đồng thời. Phương pháp của ông được mô tả là tập trung chặt chẽ nhưng vô cùng bổ ích, mang đến cho người đọc những hiểu biết mới mẻ trên gần như từng trang sách. Phong cách viết của Burnham được đánh giá cao về sự dễ tiếp cận, giúp những chủ đề phức tạp trở nên dễ hiểu hơn. Ông còn lồng ghép các tham chiếu văn hóa và ví dụ thực tiễn để minh họa cho các luận điểm của mình. Dù một số độc giả cho rằng một vài ví dụ có thể được giải thích kỹ hơn, nhưng chuyên môn sâu rộng của Burnham về chủ đề này vẫn hiện rõ xuyên suốt cuốn sách.

Listen
Now playing
Async JavaScript
0:00
-0:00
Now playing
Async JavaScript
0:00
-0:00
1x
Voice
Speed
Dan
Andrew
Michelle
Lauren
1.0×
+
200 words per minute
Queue
Home
Swipe
Library
Get App
Create a free account to unlock:
Recommendations: Personalized for you
Requests: Request new book summaries
Bookmarks: Save your favorite books
History: Revisit books later
Ratings: Rate books & see your ratings
200,000+ readers
Try Full Access for 7 Days
Listen, bookmark, and more
Compare Features Free Pro
📖 Read Summaries
Read unlimited summaries. Free users get 3 per month
🎧 Listen to Summaries
Listen to unlimited summaries in 40 languages
❤️ Unlimited Bookmarks
Free users are limited to 4
📜 Unlimited History
Free users are limited to 4
📥 Unlimited Downloads
Free users are limited to 1
Risk-Free Timeline
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Aug 12,
cancel anytime before.
Consume 2.8x More Books
2.8x more books Listening Reading
Our users love us
200,000+ readers
"...I can 10x the number of books I can read..."
"...exceptionally accurate, engaging, and beautifully presented..."
"...better than any amazon review when I'm making a book-buying decision..."
Save 62%
Yearly
$119.88 $44.99/year
$3.75/mo
Monthly
$9.99/mo
Start a 7-Day Free Trial
7 days free, then $44.99/year. Cancel anytime.
Scanner
Find a barcode to scan

Settings
General
Widget
Loading...