{"size": 891, "ext": "js", "max_stars_repo_path": "nodejs-async-tutorial/async-broken.js", "max_stars_repo_name": "hustbill/learn-node", "max_stars_repo_stars_event_min_datetime": "2015-11-07T12:34:55.000Z", "max_stars_repo_stars_event_max_datetime": "2015-11-07T12:34:55.000Z", "max_issues_repo_path": "nodejs-async-tutorial/async-broken.js", "max_issues_repo_name": "hustbill/learn-node", "max_issues_repo_issues_event_min_datetime": "2019-10-01T23:25:51.000Z", "max_issues_repo_issues_event_max_datetime": "2019-10-01T23:25:51.000Z", "max_forks_repo_path": "nodejs-async-tutorial/async-broken.js", "max_forks_repo_name": "hustbill/node-in-action", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 24.0810810811, "max_line_length": 62, "alphanum_fraction": 0.6947250281}
function doSomethingOnceAllAreDone(){
    console.log("Everything is done.");
}

function Item(delay){
    this.delay = delay;
}

Item.prototype.someAsyncCall = function(callback){
    setTimeout(function(){
        console.log("Item is done.");
        if(typeof callback === "function") callback();
    }, this.delay);
};

var items = [];
items.push(new Item(1000));
items.push(new Item(200));
items.push(new Item(500));




// Loop through some items
items.forEach(function(item){
  // Call an asynchronous function (often a save() to MongoDB)
  item.someAsyncCall();
});
 
// Note: at this point we've fired off a bunch of async calls
// but they're probably not all done executing yet
 
// This function is meant to be called once all the async
// calls above are done, but we don't know if/when they are,
// and therein lies the problem with this approach
doSomethingOnceAllAreDone();
