Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created February 18, 2015 13:49
Show Gist options
  • Save dtinth/1ee33f24c678cf08bce9 to your computer and use it in GitHub Desktop.
Save dtinth/1ee33f24c678cf08bce9 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<h1>This counter should keep counting smoothly: <span id="counter"></span></h1>
<script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.min.js"></script>
<script>
// counter to make it easy to see when the application freezes
var counter = 0
requestAnimationFrame(function frame() {
counter += 1
document.querySelector('#counter').innerHTML = counter
requestAnimationFrame(frame)
})
// x is a promise that resolves after 1 second
var x = new Promise(function(resolve) {
setTimeout(resolve, 1000)
})
// y returns a promise that does some task (which depends on x)
function y(task) {
return x.then(function() { throw new Error('Rejected ' + task) })
}
// create 1000 tasks
var tasks = []
for (var i = 0; i < 1000; i ++) {
tasks.push('Task ' + i)
}
// run it all
Promise.all(tasks.map(function(task) { return y(task) }))
.then(function(x) { console.log('ok') })
.catch(function(e) { console.log('rejected by ' + e) })
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment