Skip to content

Instantly share code, notes, and snippets.

@mezigh
Created June 10, 2015 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mezigh/18727cec8acf5eea96cd to your computer and use it in GitHub Desktop.
Save mezigh/18727cec8acf5eea96cd to your computer and use it in GitHub Desktop.
JS Bin the Promise in JS // source http://jsbin.com/zuquce
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="the Promise in JS">
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<button id="trigger">Click It</button>
<div id="log"><h1>Logs:</h1></div>
<script id="jsbin-javascript">
var comptePromesse = 0;
var sentence = "On verra bien à la fin!";
var triggy = document.getElementById('trigger');
triggy.addEventListener('click', testPromise);
function testPromise() {
console.log(new Date().getSeconds());
var thisComptePromesse = ++comptePromesse;
var log = document.getElementById('log');
log.insertAdjacentHTML('beforeend', thisComptePromesse +
') Handling Started (<small>Début du code synchrone</small>)<br/>');
var p1 = new Promise(
function(resolve, reject) {
var mot1 = ') Promise started (<small>Début du code asynchrone</small>)<br/>';
log.insertAdjacentHTML('beforeend', thisComptePromesse + mot1);
// un code asynchrone
window.setTimeout(
function() {
resolve(thisComptePromesse);
}, 2000 + 1000);
});
p1.then(
// On affiche un message avec la valeur
function(val) {
console.log(new Date().getSeconds());
//console.log(val);
var mot2 = ') Promise fulfilled (<small>Fin du code asynchrone</small>)<br/>';
log.insertAdjacentHTML('beforeend', val + mot2 );
}).catch(function() { console.log("promesse rompue");});
log.insertAdjacentHTML('beforeend', thisComptePromesse +
') Promise made (<small>Fin du code synchrone</small>)<br/>');
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">var comptePromesse = 0;
var sentence = "On verra bien à la fin!";
var triggy = document.getElementById('trigger');
triggy.addEventListener('click', testPromise);
function testPromise() {
console.log(new Date().getSeconds());
var thisComptePromesse = ++comptePromesse;
var log = document.getElementById('log');
log.insertAdjacentHTML('beforeend', thisComptePromesse +
') Handling Started (<small>Début du code synchrone</small>)<br/>');
var p1 = new Promise(
function(resolve, reject) {
var mot1 = ') Promise started (<small>Début du code asynchrone</small>)<br/>';
log.insertAdjacentHTML('beforeend', thisComptePromesse + mot1);
// un code asynchrone
window.setTimeout(
function() {
resolve(thisComptePromesse);
}, 2000 + 1000);
});
p1.then(
// On affiche un message avec la valeur
function(val) {
console.log(new Date().getSeconds());
//console.log(val);
var mot2 = ') Promise fulfilled (<small>Fin du code asynchrone</small>)<br/>';
log.insertAdjacentHTML('beforeend', val + mot2 );
}).catch(function() { console.log("promesse rompue");});
log.insertAdjacentHTML('beforeend', thisComptePromesse +
') Promise made (<small>Fin du code synchrone</small>)<br/>');
}</script></body>
</html>
var comptePromesse = 0;
var sentence = "On verra bien à la fin!";
var triggy = document.getElementById('trigger');
triggy.addEventListener('click', testPromise);
function testPromise() {
console.log(new Date().getSeconds());
var thisComptePromesse = ++comptePromesse;
var log = document.getElementById('log');
log.insertAdjacentHTML('beforeend', thisComptePromesse +
') Handling Started (<small>Début du code synchrone</small>)<br/>');
var p1 = new Promise(
function(resolve, reject) {
var mot1 = ') Promise started (<small>Début du code asynchrone</small>)<br/>';
log.insertAdjacentHTML('beforeend', thisComptePromesse + mot1);
// un code asynchrone
window.setTimeout(
function() {
resolve(thisComptePromesse);
}, 2000 + 1000);
});
p1.then(
// On affiche un message avec la valeur
function(val) {
console.log(new Date().getSeconds());
//console.log(val);
var mot2 = ') Promise fulfilled (<small>Fin du code asynchrone</small>)<br/>';
log.insertAdjacentHTML('beforeend', val + mot2 );
}).catch(function() { console.log("promesse rompue");});
log.insertAdjacentHTML('beforeend', thisComptePromesse +
') Promise made (<small>Fin du code synchrone</small>)<br/>');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment