Created
January 6, 2012 14:30
-
-
Save jswartwood/1570839 to your computer and use it in GitHub Desktop.
Hiccup?
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> | |
<body> | |
<p>These should output "last", "ok", then "all" for 10 promises and then another 10.</p> | |
<div id="out"></div> | |
<script type="text/javascript" src="https://raw.github.com/briancavalier/when.js/dev/when.js"></script> | |
<script type="text/javascript" src="main.js"></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
var out = document.getElementById("out") | |
, lastPromise | |
; | |
for (var i = 0; i < 10; i++) { | |
(function( pos ) { | |
var andDefer = when.defer() | |
, allPromises = [ lastPromise, andDefer ] | |
; | |
when(lastPromise).then(function() { | |
out.innerHTML += "last done: " + pos + "<br>"; | |
}); | |
lastPromise = when.all(allPromises).then(function() { | |
out.innerHTML += "all done: " + pos + "<br>"; | |
}); | |
andDefer.then(function( val ) { | |
out.innerHTML += val + ": " + pos + "<br>"; | |
}); | |
setTimeout(function() { | |
andDefer.resolve("ok"); | |
}, 1000); | |
})(i); | |
} | |
setTimeout(function() { | |
out.innerHTML += "<br>"; | |
for (var i = 0; i < 10; i++) { | |
(function( pos ) { | |
var andDefer = when.defer() | |
, allPromises = [ lastPromise, andDefer ] | |
; | |
when(lastPromise).then(function() { | |
out.innerHTML += "last done: " + pos + "<br>"; | |
}); | |
lastPromise = when.all(allPromises).then(function() { | |
out.innerHTML += "all done: " + pos + "<br>"; | |
}); | |
andDefer.then(function( val ) { | |
out.innerHTML += val + ": " + pos + "<br>"; | |
}); | |
setTimeout(function() { | |
andDefer.resolve("ok"); | |
}, 1000); | |
})(i); | |
} | |
}, 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool. Feel free to @ me on that issue if you have any more when.js questions.