GithubHelp home page GithubHelp logo

javascript-promises's Introduction

javascript-promises

what is promise and how to use it

JavaScript Promises are a programming construct used to handle asynchronous operations in JavaScript. They are a way of handling callbacks and making asynchronous code easier to read, write, and reason about.

A Promise is an object that represents the eventual completion or failure of an asynchronous operation and its resulting value. When a Promise is created, it is in one of three states:

  • Pending: The initial state of a Promise, when it is neither fulfilled nor rejected.
  • Fulfilled: The state of a Promise when it has successfully completed and returned a value.
  • Rejected: The state of a Promise when it has failed and returned an error.

A Promise can transition from the pending state to either the fulfilled or rejected state. Once a Promise is fulfilled or rejected, it cannot transition to any other state.

Promises have two main methods that allow you to handle their eventual outcome: then() and catch(). The then() method is called when a Promise is fulfilled and allows you to handle the resolved value, while the catch() method is called when a Promise is rejected and allows you to handle any errors that may have occurred.

We then use the then() method to handle the resolved value and log it to the console. We also use the catch() method to handle any errors that may have occurred and log the error message to the console.

javascript-promises's People

Contributors

amir-mansoor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.