Last active
August 29, 2015 14:20
-
-
Save ahirschberg/3a15c34aeff8278c7c5d to your computer and use it in GitHub Desktop.
Useful example of base variables referenced in a callback remaining in scope
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
// adapted from displayAppFiles() in app.js | |
function checkIfAnyFiles() { | |
var items_found = 0; // variable defined in outer function | |
var each_item = function(result) { | |
items_found++; // still in scope for the callback | |
} | |
var when_done = function () { | |
// Output depends on number of items traversed by each_item | |
console.log('found ' + items_found + ' files'); | |
if (items_found == 0) { | |
// ... write 'no files found' to the listview | |
} | |
} | |
enumerateFiles({ // ... , | |
func_each: each_item, // functions are only called inside #enumerateFiles | |
func_done: when_done | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment