Junior — Middle
Can you list types of asynchronous calls or methods you have encountered?
sobes.tech AI
Answer from AI
In Node.js, asynchronous calls can be implemented in several ways:
- Callbacks — the classic method, where a function takes another function that is called after the operation completes.
- Promises — an object representing the result of an asynchronous operation, allowing chaining with
.then()and.catch(). - async/await — syntactic sugar over promises, enabling writing asynchronous code in a synchronous style.
- EventEmitter — an event mechanism, often used for asynchronous event notification.
- Streams — allow working with data in parts asynchronously.
Example with async/await:
async function fetchData() {
try {
const data = await someAsyncOperation();
console.log(data);
} catch (error) {
console.error(error);
}
}