Promises: Promises are a way to handle asynchronous operations in Node.js. They provide a cleaner and more organized way to handle complex asynchronous code flows. Here's an example of how to create and use a Promise in Node.js:
javascript
function getData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = [1, 2, 3, 4, 5];
resolve(data);
}, 1000);
});
}
getData()
.then((data) => {
console.log(data);
})
.catch((err) => {
console.error(err);
});
No comments:
Post a Comment