๐—”๐˜€๐˜†๐—ป๐—ฐ๐—ต๐—ฟ๐—ผ๐—ป๐—ผ๐˜‚๐˜€ ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜: Flashcards

(6 cards)

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

What are Web Workers, and when should you use them?

A

โ€œWeb Workers are JavaScript scripts that run in background threads, separate from the main thread, enabling parallel execution without blocking the UI. Theyโ€™re ideal for CPU-intensive tasks like data processing or complex calculations.

For example:

// main.js
const worker = new Worker('worker.js');
worker.postMessage([1, 2, 3]);
worker.onmessage = (e) => console.log(e.data); // Outputs processed data

// worker.js
self.onmessage = (e) => {
  const result = e.data.map(x => x * 2);
  self.postMessage(result);
};

Use Web Workers for tasks like image processing or large dataset computations, but not for DOM manipulation, as they lack DOM access. As an SDET, Iโ€™d test Web Workers to ensure data is correctly passed, processed, and returned without impacting UI performance.โ€

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

How does setTimeout and setInterval work under the hood?

A

โ€œsetTimeout schedules a function to run after a specified delay, while setInterval runs a function repeatedly at a fixed interval. Both use the browserโ€™s or Node.jsโ€™s timer APIs, managed by the event loop. The callback is placed in the task queue after the delay, but it only executes when the call stack is empty.

For example:

setTimeout(() => console.log('Delayed'), 1000); // Runs after ~1s
setInterval(() => console.log('Every second'), 1000); // Runs every ~1s

Delays arenโ€™t guaranteed if the main thread is busy. As an SDET, Iโ€™d test setTimeout for correct timing under load and setInterval for consistent intervals, ensuring no callback pileup.โ€

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

Explain Microtasks and Macrotasks in JavaScript.

A

โ€œJavaScriptโ€™s event loop processes tasks in two queues: Macrotasks and Microtasks. Macrotasks include events like setTimeout, setInterval, and I/O operations, processed one per loop iteration. Microtasks, like Promise callbacks and queueMicrotask, are higher-priority and execute immediately after the current macrotask, before the next macrotask.

For example:

console.log('Start');
setTimeout(() => console.log('Timeout'), 0); // Macrotask
Promise.resolve().then(() => console.log('Promise')); // Microtask
console.log('End');
// Outputs: Start, End, Promise, Timeout

Microtasks take precedence, which can delay macrotasks. As an SDET, Iโ€™d test task ordering to ensure async code executes in the expected sequence and avoid starvation of macrotasks.โ€

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

What is Promise.all(), and how does it differ from Promise.race()?

A

Response:

โ€œPromise.all() takes an array of promises and returns a new promise that resolves with an array of all resolved values when all input promises resolve, or rejects with the first rejection. Promise.race() resolves or rejects as soon as any one of the input promises settles.

For example:

const p1 = Promise.resolve('One');
const p2 = new Promise(resolve => setTimeout(() => resolve('Two'), 1000));

// Promise.all
Promise.all([p1, p2]).then(values => console.log(values)); // ['One', 'Two'] after 1s

// Promise.race
Promise.race([p1, p2]).then(value => console.log(value)); // 'One' immediately

As an SDET, Iโ€™d test Promise.all for handling all resolutions and rejections, and Promise.race to ensure it captures the fastest promise correctly.โ€

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

How do you handle errors in async/await?

A

โ€œErrors in async/await are handled using try/catch blocks within an async function, catching errors from awaited promises. This makes error handling cleaner than promise .catch() chains.

For example:

async function fetchData() {
  try {
    const result = await new Promise((resolve, reject) => {
      setTimeout(() => reject('Error occurred'), 1000);
    });
  } catch (error) {
    console.log(error); // Outputs 'Error occurred'
  }
}
fetchData();

You can also use a global .catch() on the async functionโ€™s promise if needed. As an SDET, Iโ€™d test error scenarios to ensure try/catch handles all rejection cases and verify fallback logic works correctly.โ€

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