Skip to content

Instantly share code, notes, and snippets.

@bathtimefish
Last active December 27, 2015 13:59
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 bathtimefish/7337193 to your computer and use it in GitHub Desktop.
Save bathtimefish/7337193 to your computer and use it in GitHub Desktop.
WHATWG PromiseのChrome実装を確認したページ。sleepっぽい処理を書いてみた。 http://dom.spec.whatwg.org/#promises
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style> body,input,button { font-size:1.8em; } </style>
</head>
<body>
<button id="start">Start</button>
<div id="monitor"></div>
<script>
// running on Chrome32
document.getElementById('start').addEventListener('click', function() {
document.getElementById('monitor').innerText = 'start';
Promise.all([promiseWait('first', 1), promiseWait('second', 2), promiseWait('third', 3)])
.then(function(result) {
console.log(result);
document.getElementById('monitor').innerText = result;
});
});
function promiseWait(message, sec) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
document.getElementById('monitor').innerText = message;
resolve(message);
}, sec * 1000);
});
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style> body,input,button { font-size:1.8em; } </style>
</head>
<body>
<input id="sec" type="number" value="3" min="1">
<button id="start">Start</button>
<div id="monitor"></div>
<script>
// running on Chrome32
document.getElementById('start').addEventListener('click', function() {
var sec = document.getElementById('sec').value;
document.getElementById('monitor').innerText = 'start';
promiseWait(sec)
.then(function() {
document.getElementById('monitor').innerText = 'time is up';
});
});
function promiseWait(sec) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, sec * 1000);
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment