A Node.js application needs to resolve a hostname to its IP address. Which of the following approaches is most robust in handling potential errors?

Node.js interview question for Advanced practice.

Answer

Using dns.promises.resolve4() inside an async function with a try...catch block

Explanation

Option C is the most robust because using promises with async/await and a try...catch block provides a clean, structured way to handle both success and failure cases. Options A and D lack any error handling, which is unsafe for production code. Option B is better but can still lead to unhandled exceptions if the callback itself has an error, whereas try...catch provides more comprehensive protection.

Related Questions