Asynchronous JavaScript Flashcards

Callbacks Promises Async/Await Event Loop & Microtasks Error Handling in Async Code (39 cards)

1
Q

What is a callback in JavaScript?

A

A callback is a function passed as an argument to another function and invoked after the parent function completes.

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

What is a Promise in JavaScript?

A

A Promise represents a value that may be available now, later, or never, and handles asynchronous operations with then() and catch().

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

What is async/await in JavaScript?

A

Async/await is syntactic sugar over Promises that allows writing asynchronous code in a synchronous style using the ‘async’ and ‘await’ keywords.

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

What are the advantages of callbacks?

A

They are simple and foundational for asynchronous programming.

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

What are the disadvantages of callbacks?

A

They can lead to ‘callback hell’, difficult debugging, and poor error handling.

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

What are the advantages of Promises?

A

They improve readability, support chaining, and provide cleaner error handling.

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

What are the disadvantages of Promises?

A

They can become hard to manage in deeply nested or conditional async flows.

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

What are the advantages of async/await?

A

They simplify asynchronous code, avoid chaining, and are easier to read and debug.

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

What are the disadvantages of async/await?

A

They require Promises under the hood and can cause blocking if misused.

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

What is a best practice with callbacks?

A

Avoid deep nesting by breaking code into separate named functions.

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

What is a best practice with Promises?

A

Always return Promises and handle both resolve and reject cases with then() and catch().

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

What is a best practice with async/await?

A

Use try/catch blocks for error handling and avoid mixing with then()/catch().

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

What is a use case for callbacks?

A

Used in older APIs like fs.readFile or event listeners in Node.js.

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

What is a use case for Promises?

A

API calls, database queries, and file operations where sequencing and chaining are needed.

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

What is a use case for async/await?

A

Handling complex flows with multiple asynchronous steps like HTTP requests inside loops.

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

How do callbacks impact system design?

A

Can make codebase harder to scale and maintain due to nested structures and error propagation.

17
Q

How do Promises affect system design?

A

Encourage modular, predictable async workflows and help build more maintainable APIs.

18
Q

How does async/await affect system design?

A

Promotes clean, linear asynchronous flows but can lead to blocking if not handled carefully.

19
Q

Provide an example of a callback.

A

fs.readFile(‘file.txt’, (err, data) => { if (err) throw err; console.log(data); });

20
Q

Provide an example of a Promise.

A

fetch(‘url’).then(response => response.json()).then(data => console.log(data)).catch(err => console.error(err));

21
Q

Provide an example of async/await.

A

async function loadData() { try { const res = await fetch(‘url’); const data = await res.json(); console.log(data); } catch (err) { console.error(err); } }

22
Q

What are architectural implications of callbacks?

A

Callback-heavy codebases are hard to debug, test, and extend without introducing bugs or race conditions.

23
Q

What are architectural implications of Promises?

A

Allow separation of concerns and scalable abstraction of asynchronous logic.

24
Q

What are architectural implications of async/await?

A

Encourages synchronous-style structure for asynchronous logic, improving modularity and readability.

25
How do callbacks perform under load?
They perform well but are harder to trace and debug, leading to more runtime errors.
26
Are Promises fault-tolerant?
Yes, if implemented with proper catch blocks, Promises can isolate and propagate errors cleanly.
27
Is async/await fault-tolerant?
Yes, when used with try/catch blocks, it provides clear and manageable error handling.
28
How do you debug callbacks?
Use console logs, breakpoints, and name all functions to improve stack trace readability.
29
How to monitor Promise-based flows?
Use async hooks, log Promise states, and track uncaught rejections with global handlers.
30
What is a real-world tradeoff with callbacks?
Performance is high, but maintenance and scalability suffer.
31
What is a real-world tradeoff with Promises?
Improves readability and flow control, but requires careful chaining to avoid nesting.
32
What is a real-world tradeoff with async/await?
Improves clarity but may hide asynchronous behavior, causing unintentional blocking.
33
Interview Question: What is callback hell and how do you avoid it?
Callback hell is deeply nested callbacks; it can be avoided with named functions, Promises, or async/await.
34
Interview Question: What are the Promise states?
Pending, Fulfilled, and Rejected.
35
Interview Question: What happens if you await a non-Promise value?
It gets implicitly wrapped in a resolved Promise.
36
Interview Question: How do you handle errors in async/await?
Wrap the await call in a try/catch block.
37
What is a common gotcha with callbacks?
Forgetting to handle errors or calling the callback multiple times.
38
What is a gotcha with Promises?
Not returning from a .then() can break the Promise chain.
39
What is a gotcha with async/await?
Forgetting to use 'await' can lead to unhandled Promise objects and bugs.