DEV Community

Cover image for Promises
yatharth1706
yatharth1706

Posted on

1 1

Promises

What are promises?

Its a special JavaScript object to handle asynchronous operations in an easy way.

The constructor syntax for a promise object is:

  let promise = new Promise(function(resolve, reject) {
    // Perform some asynchronous task here
  });  

The function passed to new Promise is called the executor. When new Promise is created, the executor runs automatically.

Arguments of Executor:

It contains two callbacks

  1. resolve
  2. reject

resolve(value) — if the job finished successfully, with result value.

reject(error) — if an error occurred, error is the error object.

The promise object returned by the new Promise constructor has these internal properties:

  • state — initially "pending", then changes to either "fulfilled" when resolve is called or "rejected" when reject is called.
  • result — initially undefined, then changes to value when resolve(value) called or error when reject(error) is called.

Promise States

Methods on Promise

1.then: for fetching results and errors from promises
Syntax:

      promise.then(
        function(result) { /* handle a successful result */ },
        function(error) { /* handle an error */ }
      );

Some Examples:

      let promise = new Promise(function(resolve, reject) {
        setTimeout(() => resolve("done!"), 1000);
      });

      // resolve runs the first function in .then
      promise.then(
        result => alert(result), // shows "done!" after 1 second
        error => alert(error) // doesn't run
      );

2.catch: for fetching errors from promise

Example:

      let promise = new Promise((resolve, reject) => {
        setTimeout(() => reject(new Error("Whoops!")), 1000);
      });

      // .catch(f) is the same as promise.then(null, f)
      promise.catch(alert);

3.finally: for performing cleanup operations. It will always execute no matter your callback is returning resolve callback or reject callback.

Example:

      new Promise((resolve, reject) => {
        setTimeout(() => resolve("result"), 2000)
      })
      .finally(() => alert("Promise ready"))
      .then(result => alert(result));

Link for tutorial video Click here

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (1)

Collapse
 
leoalbin profile image
leoalbin

Nice and clear! Good job. 🙂

Image of Checkly

4 Playwright Locators Explained: Which One Should You Use?

- locator('.cta'): Fast but brittle
- getByText('Click me'): User-facing, but can miss broken accessibility
- getByRole('button', { name: 'Click me' }): Most robust, best for a11y
- getByTestId('cta-button'): Stable, but ignores UX

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay