DEV Community

Abinaya V
Abinaya V

Posted on

Promise in javascript

A Promise in JavaScript is an object that represents the result of an asynchronous operation.

It helps handle tasks like:

  • API calls

  • File loading

  • Database requests

  • Timers (setTimeout)

Instead of waiting for the task to finish, JavaScript continues running other code.

Promise States

A Promise has 3 states:
Enter fullscreen mode Exit fullscreen mode
  • Pending → Initial state

  • Resolved / Fulfilled → Operation successful

  • Rejected → Operation failed

Promise Methods

Method and Purpose

.then() -> Runs when promise is successful

.catch() -> Runs when promise fails

.finally() -> Runs always

output

Callback Hell.

output

Top comments (0)