Skip to content

Pact is a custom implementation of JavaScript's Promise. The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

kartikmehta8/Pact-Custom-Promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

BANNER

Promise

A JavaScript Promise object contains both the producing code and calls to the consuming code:

let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)

  myResolve(); // when successful
  myReject();  // when error
});

// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);

When the producing code obtains the result, it should call one of the two callbacks:

Result Call
Success myResolve(result value)
Error myReject(error object)

Promise Object Properties

A JavaScript Promise object can be:

  • Pending
  • Fulfilled
  • Rejected

The Promise object supports two properties: state and result.

While a Promise object is "pending" (working), the result is undefined.

When a Promise object is "fulfilled", the result is a value.

When a Promise object is "rejected", the result is an error object.

myPromise.state myPromise.result
"pending" undefined
"fulfilled" a result value
"rejected" an error object

Pact is a custom implementation of JavaScript's Promise.

About

Pact is a custom implementation of JavaScript's Promise. The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

Topics

Resources

Stars

Watchers

Forks