what is a promise

JavaScript
var promise = new Promise(function(resolve, reject) {
  // do some long running async thing…
  
  if (/* everything turned out fine */) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});

//usage
promise.then(
  function(result) { /* handle a successful result */ },
  function(error) { /* handle an error */ }
);We use promise to make a  AsyncFunction, cose simetime we have to wait that function give 
us some result.
Example, if we use ajax we have await ajax data or statament.
_____________________________________
Make a simple example.
_____________________________________
var asyncronus_function= (number)=>
		{
			return new Promise( (accept, reject)=>
			{
			})
		}                 
_____________________________________
this function return a promise Object, thet required a function executor
this functions (accept, reject) are defined in the executor 
function, that was needed in promise constructor.
Syntax: new Promise (executor)
executor= (accept, reject) =>{}

if function end well we return a accept(), otherwise reject()
_____________________________________
Let complete asyncronus_function
_____________________________________
var asyncronus_function= (number)=>
		{
			return new Promise( (accept, reject)=>
			{
				if(number>10)
				return accept("my first async");
				return reject("my first async error")
			})

		}
if it dont return any of this 2 function, Promise state is [PENDING] ,
if return accept is [RESOLVED]  end if return reject is [REJECTED]
_____________________________________
how we can retrieve accept or reject?
_____________________________________
there is two methods really important, that we have to consider afther we call this function
1) .then(function(error){}) is call when promise state is [RESOLVED]
2) .error(function(error){}) is call when promise state is [REJECTED]
3) do nothing if [PENDING]
_____________________________________
let call asyncronus_function()!!! 
_____________________________________
	asyncronus_function(MY_NUMBER).then(function(data)
        {
			console.log(data)
    	}).catch(error => 
        {
      			console.log(error)
    	});
		
if  MY_NUMBER> 10 ,asyncronus_function print data : OUTPUT my first async
if MY_NUMBER<10 , asyncronus_function print error : OUTPUT my first async error
HOPE it halp and have a nice day! 


//example
function tetheredGetNumber(resolve, reject) {
  try {
    setTimeout( 
      function() {
        const randomInt = Date.now();
        const value = randomInt % 10;
        try { 
          if(value >= THRESHOLD_A) {
            throw new Error(`Too large: ${value}`);
          } 
        } catch(msg) {
            reject(`Error in callback ${msg}`); 
        }
      resolve(value);
      return;
    }, 500);
  } catch(err) {
    reject(`Error during setup: ${err}`);
  }
  return;
}A promise is an object that may produce a single value some time in the future: 
either a resolved value, or a reason that it’s not resolved var promise = new Promise(function(resolve, reject) {
  // do some long running async thing…
  
  if (/* everything turned out fine */) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});// Promise is a special type of object that helps you work with asynchronous operations.
// Many functions will return a promise to you in situations where the value cannot be retrieved immediately.

const userCount = getUserCount();
console.log(userCount); // Promise {<pending>}

// In this case, getUserCount is the function that returns a Promise. If we try to immediately display the value of the userCount variable, we get something like Promise {<pending>}.
// This will happen because there is no data yet and we need to wait for it.
Source

Also in JavaScript: