NA +91-8727899942 thecoderjob@gmail.com

CHAPTER - 11

What is promises in node js

In Node.js, promises are a way to handle asynchronous operations.

They provide a cleaner alternative to callbacks and make it easier to write asynchronous code that's easier to read, write, and maintain.

Here's is the Code !!

const myPromise = new Promise((resolve, reject) => {
  // Async operation
  setTimeout(() => {
    // Resolve the promise
    resolve('Operation completed successfully!');
    // Or reject the promise
    // reject(new Error('Operation failed!'));
  }, 2000);
});

myPromise
  .then((result) => {
    console.log(result); // Output: "Operation completed successfully!"
  })
  .catch((error) => {
    console.error(error); // Output: "Operation failed!"
  });

Chapter 10

What is callbacks in node js

Previous chapter

Chapter 12

What is promises chaining in node Js

Next chapter

Get In Touch

NA

thecoderjob@gmail.com

+91-8727899942

Popular Links

© www.thecoderjob.com. All Rights Reserved.               Designed by HTML Codex