DEV Community

Raja B
Raja B

Posted on

Promise

A Promise in JavaScript is an object that represents the future result of an asynchronous operation, like fetching data, waiting for a timer, or reading a file.

It can be in one of three states: pending, fulfilled, or rejected.

promise

  • new Promise(...) creates a new promise

  • res means resolve, rej means reject

  • rej() makes the promise fail

  • .then() runs only when the promise is fulfilled

  • .catch() runs when the promise is rejected


promise1


  • Analysis() represents an analysis operation that takes 3 seconds. After the analysis finishes, res() tells JavaScript: "The work is completed successfully."

  • This waits for 3000 milliseconds = 3 seconds.

promise3

Reference

Top comments (0)