Skip to content

Instantly share code, notes, and snippets.

@iamdvr
Last active June 4, 2018 14:13
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 iamdvr/ff16cfd6e357738d071495ae6047c368 to your computer and use it in GitHub Desktop.
Save iamdvr/ff16cfd6e357738d071495ae6047c368 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<textarea rows="10" cols="50" name="console">N/A</textarea>
<input type="button" onclick="javascript:callSomeAsyncThing();" value="ClickMe" />
<input type="button" onclick="javascript:clearData();" value="Clear" />
<script id="jsbin-javascript">
var clearData = function() {
document.getElementsByName("console")[0].value = "";
};
var delayFun = function (delay) {
var preTime = new Date().getTime();
var loopCnt = 0;
while (true) {// noprotect
var diff = (new Date().getTime()) - preTime;
if (diff > delay) {
console.log( diff + ' - ' + delay + ' : ' + loopCnt);
break;
}else{
loopCnt++;
}
}
};
var someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// this will throw, x does not exist
var x = 4;
var preDate = new Date();
delayFun(300);
var postDate = new Date();
resolve(preDate + "\r\r" + postDate);
});
};
var callSomeAsyncThing = function() {
someAsyncThing()
.then(function(data) {
document.getElementsByName("console")[0].value = data;
})
.catch(function(error) {
console.log('oh no', error);
});
};
</script>
<script id="jsbin-source-javascript" type="text/javascript">
var clearData = function() {
document.getElementsByName("console")[0].value = "";
};
var delayFun = function (delay) {
var preTime = new Date().getTime();
var loopCnt = 0;
while (true) {// noprotect
var diff = (new Date().getTime()) - preTime;
if (diff > delay) {
console.log( diff + ' - ' + delay + ' : ' + loopCnt);
break;
}else{
loopCnt++;
}
}
};
var someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// this will throw, x does not exist
var x = 4;
var preDate = new Date();
delayFun(300);
var postDate = new Date();
resolve(preDate + "\r\r" + postDate);
});
};
var callSomeAsyncThing = function() {
someAsyncThing()
.then(function(data) {
document.getElementsByName("console")[0].value = data;
})
.catch(function(error) {
console.log('oh no', error);
});
};
</script></body>
</html>
var clearData = function() {
document.getElementsByName("console")[0].value = "";
};
var delayFun = function (delay) {
var preTime = new Date().getTime();
var loopCnt = 0;
while (true) {// noprotect
var diff = (new Date().getTime()) - preTime;
if (diff > delay) {
console.log( diff + ' - ' + delay + ' : ' + loopCnt);
break;
}else{
loopCnt++;
}
}
};
var someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// this will throw, x does not exist
var x = 4;
var preDate = new Date();
delayFun(300);
var postDate = new Date();
resolve(preDate + "\r\r" + postDate);
});
};
var callSomeAsyncThing = function() {
someAsyncThing()
.then(function(data) {
document.getElementsByName("console")[0].value = data;
})
.catch(function(error) {
console.log('oh no', error);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment