Created
March 17, 2014 18:29
-
-
Save kylehill/9605347 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function asyncFunction (param) { | |
// Scope local variables to this function | |
// param is also both locally scoped | |
var n = param.name | |
var e = param.extension | |
// Simulate something asynchronous happening | |
setTimeout(function(){ | |
console.log("Downloaded file: " + n + "." + e) | |
}, 500 + (Math.random() * 500)) | |
} | |
var fonts = [ | |
{ name: "kyle", extension: "woff" }, | |
{ name: "eli", extension: "woff" }, | |
{ name: "clownpenis", extension: "fart" } | |
] | |
// You can either do this through a standard for loop... | |
for (var i = 0; i < fonts.length; i++) { | |
console.log("Downloading file (for loop): " + fonts[i].name + "." + fonts[i].extension) | |
asyncFunction(fonts[i]) | |
} | |
// Or you can do this through functional programming | |
fonts.forEach(function(font){ | |
console.log("Downloading file (functional): " + font.name + "." + font.extension) | |
asyncFunction(font) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment