Skip to content

Instantly share code, notes, and snippets.

@jtwb
Created February 13, 2013 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtwb/4780537 to your computer and use it in GitHub Desktop.
Save jtwb/4780537 to your computer and use it in GitHub Desktop.
Asynchronous programming basics
// Time elapsed: 0µs
var a = 200000000000;
for (var i = 1000; i; i--){
a = a / 3;
}
// Time elapsed: 1µs, probably less
var exampleData = '';
$.getJSON(
'http://example.com/data.json',
function(data){
exampleData = data;
// Time elapsed: ~200000µs, after ajax request completes
// Value of exampleData: { live data }
// Second execution block terminates here
})
);
// We have now fired an ajax request and registered code to execute on ajax complete
// Time elapsed: still 1µs
// Value of exampleData: ''
// Execution block terminates here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment