Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Created October 20, 2009 11:45
Show Gist options
  • Save jakearchibald/214198 to your computer and use it in GitHub Desktop.
Save jakearchibald/214198 to your computer and use it in GitHub Desktop.
function getFilterOpts(type, item, callback) {
var requestUrl = "/api/plugin:"+type+"/config?path=" +item;
var str = '';
// get json data from config
glow.net.get(requestUrl, {
onLoad: function(response) {
var data = response.json();
str += '<p>'+data.a_value+'</p>';
// simplified example of what I need to add to var str.
// now call the callback
callback(str);
},
onError: function(response) {
alert("Error getting file: " + response.statusText());
// you may want to replace the above with...
callback('Argh, it all went wrong');
},
useCache: true
});
// you can't return str here as it hasn't been created yet
}
// usage:
getFilterOpts('blah', 'blah', function(str) {
console.log(str);
});
// because your callback is async, this next line will run before the callback
console.log('This happens before the callback');
// when the callback runs is dependant on how long the server takes to respond
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment