Async/await: Async/await is a newer way to handle asynchronous operations in Node.js. It allows you to write asynchronous code that looks and behaves like synchronous code, making it easier to read and understand. Here's an example of how to use async/await to handle a Promise in Node.js:
javascript
function getData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = [1, 2, 3, 4, 5];
resolve(data);
}, 1000);
});
}
async function main() {
try {
const data = await getData();
console.log(data);
} catch (err) {
console.error(err);
}
}
main();
No comments:
Post a Comment