Wednesday, August 14, 2019

How to create an asynchronic Javascript Promise

The Application Cache of HTML5

by carmel schvartzman
In this page we see how to create an asynchronic Javascript Promise. We use Jquery and HTML5.

How to create an asynchronic Javascript Promise



<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    
    //////  this line uses the Promise :
    fnPromise('Hello').then((url) => { console.warn(`DATA = ${url}`); },(e) => { alert('ERROR = ' + e); });
    
  });
});

 //////  this is the Promise :
const fnPromise = (url) => { 

return new Promise((resolve,reject) => {    
    if(url !== null) resolve(url);
        else reject('Bad request');
    }
    );
}

</script>
</head>
<body>
<button>Call a Promise and get the result back</button>
</body>
</html>


That’s all: enjoy HTML5!!!

עריכה: כרמל שוורצמן