Node.js / NestJS

Asynchronous Programming

Callbacks, Promises, async/await, error handling, Promise.all, Promise.race

25 คำถามสัมภาษณ์·
Junior
1

In NestJS, how to make a controller method asynchronous?

คำตอบ

Add the async keyword before the method and it will automatically return a Promise. NestJS automatically handles Promise resolution and sends the result to the client. You can also directly return a Promise without async if needed. NestJS also supports RxJS Observables. Using async/await makes the code more readable for asynchronous operations (DB, external APIs, etc.).

2

What is the main problem with callbacks in JavaScript?

คำตอบ

Callback hell (pyramid of doom) occurs when multiple asynchronous operations are nested, creating code that is difficult to read and maintain. Each indentation level adds complexity. Promises and async/await were introduced to solve this problem by allowing more linear asynchronous code writing.

3

What is a Promise in JavaScript?

คำตอบ

A Promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value. It can be in three states: pending, fulfilled (resolved successfully), or rejected (failed with an error). Promises allow chaining asynchronous operations with .then() and .catch().

4

What are the three possible states of a Promise?

5

How to handle errors with Promises?

+22 คำถามสัมภาษณ์

เชี่ยวชาญ Node.js / NestJS สำหรับการสัมภาษณ์ครั้งถัดไป

เข้าถึงคำถามทั้งหมด flashcards แบบทดสอบเทคนิค แบบฝึกหัด code review และตัวจำลองสัมภาษณ์

เริ่มใช้ฟรี