I came to Javascript from Ruby/Scheme/Haskell, so when I had to call the Yahoo Search API with a callback, sometimes with two or more searches concurrently, I thought, oh, I'll store the continuation, because they only allow /[A-Za-z0-9.]+/ as characters in the callback.
var fns = [];
function gensym() { return $R(0,20).toArray().map(function(){return String.fromCharCode(Math.random()*26+65)}).join('') }
function storefunc(f) { var g = gensym(); fns[g] = f; return 'fns.'+g }
function jsonk(url,hash,k) {
var s = document.createElement('script');
I learned gensym and web-read-k in Scheme at Brown (thanks, Shriram!), and jsonk turned out to be pretty useful, because the same comtinuation-passing style that's available when you do XHRs now becomes available when you do json requests from a different domain, if they have a callback parameter. (I brought this code to Songkick, and we shortened callback to k.)
fns stores continuations.
gensym: make an array of 20 random characters (make an array of 20 #s, map each to a random character).
storefunc: continuation -> string, and store the continuation in fns, such that eval(string)==continuation
jsonk: make a query string from the url, the parameters supplied, and the continuation (change 'callback' to whatever the api calls it), and then add that script to the page, returning the url for debugging purposes.
you could also have a timeout after 30 seconds to say "something went wrong", and that might not be much more code, just set a timeout, and wrap k in jsonk to a function that first stops the delayed error message and then calls k, but that's an exercise for the reader.
var fns = [];
function gensym() { return $R(0,20).toArray().map(function(){return String.fromCharCode(Math.random()*26+65)}).join('') }
function storefunc(f) { var g = gensym(); fns[g] = f; return 'fns.'+g }
function jsonk(url,hash,k) { var s = document.createElement('script');
}I learned gensym and web-read-k in Scheme at Brown (thanks, Shriram!), and jsonk turned out to be pretty useful, because the same comtinuation-passing style that's available when you do XHRs now becomes available when you do json requests from a different domain, if they have a callback parameter. (I brought this code to Songkick, and we shortened callback to k.)