Asynchronous Javascript Flashcards

1
Q

What is the difference between Asynchronous vs Sync in Javascript?

A

Synchronous JavaScript: As the name suggests synchronous means to be in a sequence, i.e. every statement of the code gets executed one by one. So, basically a statement has to wait for the earlier statement to get executed.

Asynchronous JavaScript: Asynchronous code allows the program to be executed immediately where the synchronous code will block further execution of the remaining code until it finishes the current one.

JavaScript does is, it passes the runtime function(like “setTImeout”) to web API and then we keep on running our code as usual.
So it does not block the rest of the code from executing and after all the code its execution, it gets pushed to the call stack and then finally gets executed.

This is what happens in asynchronous JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are promises?

A

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do we pass inside a promise?

A

new Promise(function(resolve, reject) {
// executor (the producing code, “singer”)
});

The function passed to new Promise is called the executor. When new Promise is created, the executor runs automatically.

Its arguments resolve and reject are callbacks provided by JavaScript itself. Our code is only inside the executor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is then in promise?

A

then
The most important, fundamental one is .then.

they are returned when the resolve returns the data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is catch in promise?

A

If we’re interested only in errors, then we can use null as the first argument: .then(null, errorHandlingFunction). Or we can use .catch(errorHandlingFunction), which is exactly the same:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Async/Await?

A

It avoids using promise chaining and make asynchronous task perform much faster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Job Queue?

A

ES6 introduced the concept of the Job Queue, which is used by Promises (also introduced in ES6). It’s a way to execute the result of an async function as soon as possible, rather than being put at the end of the call stack.

So whenever we use Promise for an async task, after the promise is resolved, the callback associated with the promise is pushed to the Job Queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Different ways to execute multiple promises?

A

Sequence: default approach using one async after another or promise chaining.

Race:
The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.

Parallel:
Promise.all - The Promise.all method takes an array of promises and returns a single promise that resolves to an array of the results of the input promises.

Settlement:
Promise.allSettled is similar to Promise.all except that it returns a promise that resolves no matter if any or all of the input promises are fulfilled or rejected.

Any:
Promise.any also takes an array of promises and returns a single promise that resolves as soon as any of the promises in the input array fulfills.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Difference between Promise.any vs. Promise.race?

A

Promise.any cares only about the fastest one to resolve whereas Promise.race cares about the fastest one that either resolves or rejects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where do Web API run?

A

They run as threads. They run as web workers.
In node, it’s worker threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a web worker?

A

A web worker is a javascript function running on a different thread.
They don’t have access to many functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly