Asynchronous JavaScript Flashcards
Callbacks Promises Async/Await Event Loop & Microtasks Error Handling in Async Code (39 cards)
What is a callback in JavaScript?
A callback is a function passed as an argument to another function and invoked after the parent function completes.
What is a Promise in JavaScript?
A Promise represents a value that may be available now, later, or never, and handles asynchronous operations with then() and catch().
What is async/await in JavaScript?
Async/await is syntactic sugar over Promises that allows writing asynchronous code in a synchronous style using the ‘async’ and ‘await’ keywords.
What are the advantages of callbacks?
They are simple and foundational for asynchronous programming.
What are the disadvantages of callbacks?
They can lead to ‘callback hell’, difficult debugging, and poor error handling.
What are the advantages of Promises?
They improve readability, support chaining, and provide cleaner error handling.
What are the disadvantages of Promises?
They can become hard to manage in deeply nested or conditional async flows.
What are the advantages of async/await?
They simplify asynchronous code, avoid chaining, and are easier to read and debug.
What are the disadvantages of async/await?
They require Promises under the hood and can cause blocking if misused.
What is a best practice with callbacks?
Avoid deep nesting by breaking code into separate named functions.
What is a best practice with Promises?
Always return Promises and handle both resolve and reject cases with then() and catch().
What is a best practice with async/await?
Use try/catch blocks for error handling and avoid mixing with then()/catch().
What is a use case for callbacks?
Used in older APIs like fs.readFile or event listeners in Node.js.
What is a use case for Promises?
API calls, database queries, and file operations where sequencing and chaining are needed.
What is a use case for async/await?
Handling complex flows with multiple asynchronous steps like HTTP requests inside loops.
How do callbacks impact system design?
Can make codebase harder to scale and maintain due to nested structures and error propagation.
How do Promises affect system design?
Encourage modular, predictable async workflows and help build more maintainable APIs.
How does async/await affect system design?
Promotes clean, linear asynchronous flows but can lead to blocking if not handled carefully.
Provide an example of a callback.
fs.readFile(‘file.txt’, (err, data) => { if (err) throw err; console.log(data); });
Provide an example of a Promise.
fetch(‘url’).then(response => response.json()).then(data => console.log(data)).catch(err => console.error(err));
Provide an example of async/await.
async function loadData() { try { const res = await fetch(‘url’); const data = await res.json(); console.log(data); } catch (err) { console.error(err); } }
What are architectural implications of callbacks?
Callback-heavy codebases are hard to debug, test, and extend without introducing bugs or race conditions.
What are architectural implications of Promises?
Allow separation of concerns and scalable abstraction of asynchronous logic.
What are architectural implications of async/await?
Encourages synchronous-style structure for asynchronous logic, improving modularity and readability.