DEV Community

Discussion on: Are "asynchronous" and "parallel" the same terms in software development?

Collapse
 
pizmovc profile image
Luka • Edited

Parallel means there are multiple different things happening at the same time. You can listen to music and cook at the same time. You're doing these two things in parallel.

Asynchronous is used when you start some process, that you know will finish at some point, but you're not going to wait for it. Sending an email is asynchronous communication, because you send an email and you know that you'll get a reply some time in the future. Having a phone call is synchronous communication, since you have to wait for the other person to stop talking, so that you can talk and vice-versa.

You can combine the two. If you sent an email to several recipients, they can all be writing their replies at the same time (in parallel). But you'd still be waiting for their replies (while possibly doing other things).

An example asynchronous task is fetching some resource (some text lets say). You fire the request, then when it is finished some task happens (Promise/Callback).

console.log(1);
get('https://cdn.com/someImage').then((image) => { displayImageSize(image) });
console.log(2);

The above example would print

1
2
1232kb

An example for parallel task is processing something in the background, like calculating something that will that a long time).

const possiblePrimes = [3, 5, 1233112, 568543, 6575323445];
console.log(1);
possiblePrimes.foreach((number) => {
  // Creates a background task and starts it in 0 ms (meaning right now)
  setTimeout(() => console.log(isPrime(number)), 0);
};
console.log(2);

The above example would print 1 then 2 and them some sequence of true/false. Since the isPrime will be running in parallel the fastest result will be printed first (probably true for 3).