You are here: Foswiki>ReactJs Web>ExploringECMAScript6(ES-6)>Promises (26 Jan 2022, shss\dhakchina)Edit Attach
-- Main.shssdhakchina - 26 Jan 2022

JavaScript Promises

"I Promise a Result!" "Producing code" is code that can take some time "Consuming code" is code that must wait for the result A Promise is a JavaScript object that links producing code and consuming code

JavaScript Promise Object

A JavaScript Promise object contains both the producing code and calls to the consuming code:
Example
function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

let myPromise = new Promise(function(myResolve, myReject) {
  let x = 0;

// The producing code (this may take some time)

  if (x == 0) {
    myResolve("OK");
  } else {
    myReject("Error");
  }
});

myPromise.then(
  function(value) {myDisplayer(value);},
  function(error) {myDisplayer(error);}
);
Topic revision: r1 - 26 Jan 2022, shss\dhakchina
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback