Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var promises = [];
function async(promise) {
    setTimeout(function () {
        promise.resolve();
        if (Math.random() > 0.1) {
            var newPromise = $.Deferred();
            promises.push(newPromise);
            console.log("Added new promise. Count: " + promises.length);
            async(newPromise);
        } else {
            console.log("Done adding promises.");
        }
    }, 25);
}
console.log("--- Setting off first promise ---");
var promise = $.Deferred();
promises.push(promise);
console.log("Added new promise. Count: " + promises.length);
async(promise);
setTimeout(function() { // How to do this without an arbitary timout?
    $.when.apply($, promises).done(function () {
        console.log(promises.length + " promises finished");
    });
}, 200);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
silverwindpro
0viewers