{"size": 1570, "ext": "js", "max_stars_repo_path": "v1_nesting_callbacks.js", "max_stars_repo_name": "dhable/async-lecture", "max_stars_repo_stars_event_min_datetime": "2016-03-15T18:46:09.000Z", "max_stars_repo_stars_event_max_datetime": "2016-03-15T18:46:09.000Z", "max_issues_repo_path": "v1_nesting_callbacks.js", "max_issues_repo_name": "dhable/async-lecture", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "v1_nesting_callbacks.js", "max_forks_repo_name": "dhable/async-lecture", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 35.6818181818, "max_line_length": 85, "alphanum_fraction": 0.6140127389}
var _ = require("underscore"),
    lib = require("./lib.js");

/*
 * This is the first version of our implementation. It uses a set of 3rd party
 * APIs that have been provided to us as lib.js from our favorite travel site.
 *
 * In general, my script looks for the cheapest flight and then buys the cheapest
 * seat using my customer record. Once it's all done, I'd like to send a confirmation
 * email to my parents so they'll know when I'm going home.
 *
 * Since my implementation started small, I just decided to nest the callback
 * function declerations inline with the API methods. Before I knew it, I was in
 * callback hell!
 */
function bookFlightsForHolidays() {
    lib.fetchAvailSeatsFromDB(function(err, seats) {
        var cheapSeat = _.reduce(seats, function(currentCheapest, seat) {
            if(!currentCheapest) return seat;
            
            if (seat.price < currentCheapest.price) 
                return seat;
            else
                return currentCheapest;
        }, undefined);

        lib.fetchMyCustRecord(function(err, me) {
            lib.sellSeat(me.id, cheapSeat.seatId, function(err, success) {
                if(success)
                    lib.sendConfirmationEmail(me.id, function(err) {
                        console.log("all done");
                    });
                else
                    console.log("something when wrong trying to buy the ticket.");
            });
        });
    });
};


// Invoke the function right away to run it as a stand alone
// node.js script.
bookFlightsForHolidays();
