nc_npm
Version:
A web front end library
40 lines (39 loc) • 886 B
HTML
<html>
<body>
<div id="test-promise-log" style="border: solid 1px #ccc; padding: 1em; margin: 15px 0;">
<p>Log:</p>
</div>
</body>
<script>
var logging = document.getElementById('test-promise-log');
while(logging.children.length>1)
{
logging.removeChild(logging.children[logging.children.length-1]);
}
function log(s){
var p = document.createElement('p');
p.innerHTML = s;
logging.appendChild(p);
}
new Promise(function(resolve,reject){
log('start new Promise');
var timeout = Math.random()*2;
log('set timeout to: '+timeout+' seconds.');
setTimeout(function(){
if(timeout<1){
log('call resolve()...');
resolve('200 OK');
}
else{
log('call reject()...');
reject('timeout in '+timeout+' seconds.');
}
},timeout*1000);
}).then(function(r){
log('Done: '+r);
}).catch(function(reason){
log('Failed: '+reason);
});
</script>
</html>