In this article, I’d like to touch on async
, await
, and Promise.all
in JavaScript. First, I’ll talk about concurrency vs. parallelism and why we will be targeting parallelism in this article. Then, I’ll talk about how to use async
and await
to implement a parallel algorithm in serial and how to make it work in parallel by using Promise.all
. Finally, I’ll create an example project using Salesforce’s Lightning Web Components where I will build an art gallery using Harvard’s Art Gallery API.
I want to quickly touch on the difference between concurrency and parallelism. You can relate concurrency to how a single-threaded CPU processes multiple tasks. Single-threaded CPUs emulate parallelism by switching between processes quickly enough that it seems like multiple things are happening at the same time. Parallelism is when a CPU has multiple cores and can actually run two tasks at the exact same time. Another great example is this:
Concurrency is two lines of customers ordering from a single cashier (lines take turns ordering); Parallelism is two lines of customers ordering from two cashiers (each line gets its own cashier). [1]
#javascript