Last active
August 29, 2015 14:24
-
-
Save scottjason/fc4170c64ed5af89b36c to your computer and use it in GitHub Desktop.
This file contains 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 results = []; | |
var app = { | |
initialize: function() { | |
var arr = [ | |
this.generateMediaObj('imgs/avengers.jpg', "Avengers", "2012"), | |
this.generateMediaObj('imgs/blade_runner.jpg', "Blade Runner", "1982") | |
]; | |
/* eachLimit(arr, limit, iterator, callback) */ | |
async.eachLimit(arr, 1, this.makeRequest, this.onComplete); | |
}, | |
generateMediaObj: function(boxCover, searchName, year) { | |
var mediaObj = {}; | |
mediaObj.boxCover = boxCover; | |
mediaObj.searchName = searchName; | |
mediaObj.year = year; | |
mediaObj.genre = ""; | |
mediaObj.imdbRating = ""; | |
return mediaObj; | |
}, | |
makeRequest: function(obj, callback) { | |
var url = 'http://www.omdbapi.com/?t=' + obj.searchName + '&y=' + obj.year + '&plot=short&r=json'; | |
var request = new XMLHttpRequest(); | |
request.open('GET', url, true); | |
request.onload = onResponse; | |
request.send(); | |
function onResponse() { | |
var response = JSON.parse(this.response); | |
obj.genre = response.Genre; | |
obj.imdbRating = response.imdbRating; | |
results.push(obj); | |
/* | |
When invoked, this callback tells async.eachLimit that this element is done with and to start with the next. | |
If there are no more elems, this.onComplete will be invoked. | |
*/ | |
callback(); | |
} | |
}, | |
/* This is called when the loop is complete and all requests have been made, results is an arr of objs */ | |
onComplete: function() { | |
console.log(results); | |
} | |
}; | |
app.initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment