JSFiddle - React, Tailwind, and code Playground

by jswartwood

HTML

<script src="https://raw.github.com/briancavalier/when.js/dev/when.js"></script>
<p>These should output "last", "ok", then "all".</p>

<div id="out"></div>

JavaScript

var out = document.getElementById("out")
  , lastPromise
;

for (var i = 0; i < 10; i++) {
    (function( pos ) {
        var andDefer = when.defer()
          , kick = when.defer()
          , allPromises = [ lastPromise, kick ]
        ;
        
        setTimeout(function() {
            andDefer.then(function() {
                kick.resolve();
            });
        }, 0);
        
        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);
}