Promises, Callbacks, async/await Flashcards
What is a callback in JavaScript
A function passed as an argument to another function and executed later after work completes or an event occurs.
Why are callbacks used in JavaScript
To handle asynchronous tasks without blocking the single-threaded execution.
How do callbacks work in JavaScript
A function is passed as an argument to another function which executes it when appropriate such as after completing its work.
What are the two types of callbacks
Synchronous callbacks executed immediately and asynchronous callbacks executed after async operations.
What analogy explains callbacks
Giving a phone number to a restaurant which calls back when the order is ready.
What is a synchronous callback example
A function passed to another function and executed right away like logging a greeting then a goodbye.
What is an asynchronous callback example
Using setTimeout to delay executing a function after data fetching as simulated with a timer.
How do callback functions differ from regular functions
Callbacks are passed as arguments and rely on another function to execute them while regular functions are called directly.
What is callback hell
Nested callbacks leading to complex and hard-to-read code often fixed with Promises or Async-Await.
What are solutions to callback hell
Using Promises for cleaner async code or Async-Await to make it look synchronous.
What is a key point about callbacks
They are essential for non-blocking operations in JavaScript and avoiding callback hell with modern syntax.
How is a callback used for error handling in Node.js
Callbacks often follow the error-first pattern like function(err data) where err is checked before processing data.
What is a common array method using a callback
The forEach method takes a callback to execute on each element like
[1 2 3].forEach(num =>
console.log(num)
)
How can you pass an anonymous function as a callback
Pass a function directly as an argument like
setTimeout(() => console.log('Done') ,1000)
What is an example of a callback with parameters
Using
addEventListener(‘click’ (event) => console.log(event.target))
to log the clicked element.
How do you convert a synchronous callback to async
Use setTimeout to delay execution like callback => setTimeout(() => callback() 0).
What is a problem with nested callbacks
Deeply nested code like
step1(() => step2(() => step3()))
becomes hard to read and maintain.
How do Promises replace callbacks
Promises chain .then() methods like
fetch(url).then(data => console.log(data)).
What is an example of callback-based file reading in Node.js
fs.readFile(‘file.txt’, ‘utf8’,
(err, data) => {
if (err) throw err; console.log(data) }
)
How to handle multiple async callbacks in parallel
Use libraries like async.js or
Promise.all()
to manage execution flow.
What is a simple callback hell example
setTimeout(() => { console.log(‘1’); setTimeout(() => { console.log(‘2’) } ,1000) } ,1000).
What is a Promise in JavaScript
A promise is an object representing the eventual completion or failure of an asynchronous operation along with its resulting value
Why are Promises used
Promises solve the issue of callback hell and make asynchronous code more readable and manageable
What are the three states of a Promise
Pending Fulfilled and Rejected