Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var test;
  2.  
  3. // Synchronous
  4. test = 'before';
  5.  
  6. // Asynchronous
  7. var myFunction = function (cb) {
  8.     // Let's pretend this is network or filesystem access...
  9.     setTimeout(function () {
  10.         cb('after')
  11.     }, 1000);
  12. }
  13. myFunction(function(result) {
  14.    test = result;
  15. });
  16.  
  17. // This executes out of order, because setTimeout is async    
  18. setTimeout(function () {
  19.     console.log(test);
  20. }, 2000);
  21. console.log(test);
  22.  
  23. // Output:
  24. // > before
  25. // > after
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement