NodeJs — Asynchronous Programming with Promises and Async/Await
Node.js is asynchronous by default. Understanding Promises and async/await is essential for writing clean, non-blocking code while handling asynchronous operations like database queries or API requests.
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => resolve('Data fetched!'), 1000);
});
}
async function getData() {
const data = await fetchData();
console.log(data);
}
getData();