Last active
December 27, 2015 13:59
-
-
Save bathtimefish/7337193 to your computer and use it in GitHub Desktop.
WHATWG PromiseのChrome実装を確認したページ。sleepっぽい処理を書いてみた。
http://dom.spec.whatwg.org/#promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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