✅ 1. Core Node.js Concepts - Asynchronous Programming Flashcards

(30 cards)

1
Q

What is asynchronous programming in Node.js?

A

It allows tasks to run non-blocking and continue execution without waiting for previous operations to complete.

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

What is a callback in Node.js?

A

A function passed as an argument to another function, executed after the completion of an asynchronous operation.

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

What is callback hell?

A

It refers to nested callbacks that make the code hard to read and maintain.

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

Convert this callback function to a promise:

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

A

const readFileAsync = (file) => {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
};

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

What is a Promise?

A

An object that represents the eventual completion or failure of an asynchronous operation.

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

What are the three states of a Promise?

A

Pending: Initial state.
Fulfilled: Operation completed successfully.
Rejected: Operation failed.

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

What is the difference between Promise.all() and Promise.race()?

A

Promise.all(): Resolves when all promises resolve.
Promise.race(): Resolves as soon as one promise resolves or rejects.

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

What is the output of this code?

Promise.resolve(‘A’)
.then((res) => console.log(res))
.finally(() => console.log(‘Finally’));

A

A
Finally

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

What does async do in Node.js?

A

It marks a function to always return a promise, allowing the use of await inside it.

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

What does await do in Node.js?

A

It pauses the execution of the function until the promise resolves.

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

What is the output of this code?

async function example() {
console.log(‘A’);
await Promise.resolve();
console.log(‘B’);
}
example();
console.log(‘C’);

A

A
C
B

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

How do you handle errors in async/await?

A

Using try-catch blocks:

try {
const data = await fetchData();
} catch (error) {
console.error(error);
}

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

What is the difference between .then() and await?

A

.then(): Used with chained promises.
await: Makes the code look synchronous and easier to read.

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

What does Promise.reject() do?

A

It immediately returns a rejected promise with the specified reason.

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

What is the output of this code?

const p = new Promise((resolve, reject) => {
reject(‘Error’);
});
p.catch(err => console.log(err))
.finally(() => console.log(‘Done’));

A

Error
Done

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

What is Promise.allSettled()?

A

It waits for all promises to settle (resolve or reject) and returns their results.

17
Q

What is the output of this code?

const p1 = Promise.resolve(‘A’);
const p2 = Promise.reject(‘Error’);
Promise.allSettled([p1, p2]).then(console.log);

A

[ { status: ‘fulfilled’, value: ‘A’ },
{ status: ‘rejected’, reason: ‘Error’ } ]

18
Q

What is the purpose of util.promisify() in Node.js?

A

It converts callback-based functions into promise-based functions.

19
Q

Convert this callback to a promise using util.promisify():

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

A

const { promisify } = require(‘util’);
const readFileAsync = promisify(fs.readFile);
readFileAsync(‘file.txt’).then(console.log).catch(console.error);

20
Q

What happens if you omit await in an async function?

A

The function returns a promise but does not wait for the operation to complete.

21
Q

What is the output of this code?

async function test() {
return ‘Hello’;
}
console.log(test());

A

Promise { ‘Hello’ }

22
Q

What is the output of this code?

async function test() {
throw new Error(‘Oops!’);
}
test().catch(console.error);

23
Q

What is Promise.any() in Node.js?

A

It resolves when at least one of the promises resolves, otherwise rejects with an aggregate error.

24
Q

What is the output of this code?

Promise.any([
Promise.reject(‘Error1’),
Promise.resolve(‘Success’),
Promise.reject(‘Error2’)
]).then(console.log);

25
What is the purpose of Promise.all()?
It waits for all promises to resolve or rejects if one fails.
26
How do you create a delay with Promise?
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
27
What is the output of this code? setTimeout(() => console.log('Timeout'), 0); Promise.resolve().then(() => console.log('Promise'));
Promise Timeout Microtasks (Promises) run before timers.
28
What is the difference between process.nextTick() and setImmediate() in async operations?
process.nextTick() runs before the next event loop iteration. setImmediate() runs in the check phase.
29
How can you cancel a promise?
By using AbortController: const controller = new AbortController(); const { signal } = controller; setTimeout(() => controller.abort(), 1000);
30
What is the best practice for handling multiple async operations?
Use Promise.all() or Promise.allSettled() to handle multiple async calls efficiently.