Guest User

Untitled

a guest
Oct 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. var outerScopeVar;
  2.  
  3. var img = document.createElement('img');
  4. img.onload = function() {
  5. outerScopeVar = this.width;
  6. };
  7. img.src = 'lolcat.png';
  8. alert(outerScopeVar);
  9.  
  10. var outerScopeVar;
  11. setTimeout(function() {
  12. outerScopeVar = 'Hello Asynchronous World!';
  13. }, 0);
  14. alert(outerScopeVar);
  15.  
  16. // Example using some jQuery
  17. var outerScopeVar;
  18. $.post('loldog', function(response) {
  19. outerScopeVar = response;
  20. });
  21. alert(outerScopeVar);
  22.  
  23. // Node.js example
  24. var outerScopeVar;
  25. fs.readFile('./catdog.html', function(err, data) {
  26. outerScopeVar = data;
  27. });
  28. console.log(outerScopeVar);
  29.  
  30. // with promises
  31. var outerScopeVar;
  32. myPromise.then(function (response) {
  33. outerScopeVar = response;
  34. });
  35. console.log(outerScopeVar);
  36.  
  37. // geolocation API
  38. var outerScopeVar;
  39. navigator.geolocation.getCurrentPosition(function (pos) {
  40. outerScopeVar = pos;
  41. });
  42. console.log(outerScopeVar);
  43.  
  44. var outerScopeVar;
  45. helloCatAsync();
  46. alert(outerScopeVar);
  47.  
  48. function helloCatAsync() {
  49. setTimeout(function() {
  50. outerScopeVar = 'Nya';
  51. }, Math.random() * 2000);
  52. }
  53.  
  54. // 1. Call helloCatAsync passing a callback function,
  55. // which will be called receiving the result from the async operation
  56. helloCatAsync(function(result) {
  57. // 5. Received the result from the async function,
  58. // now do whatever you want with it:
  59. alert(result);
  60. });
  61.  
  62. // 2. The "callback" parameter is a reference to the function which
  63. // was passed as argument from the helloCatAsync call
  64. function helloCatAsync(callback) {
  65. // 3. Start async operation:
  66. setTimeout(function() {
  67. // 4. Finished async operation,
  68. // call the callback passing the result as argument
  69. callback('Nya');
  70. }, Math.random() * 2000);
  71. }
  72.  
  73. var outerScopeVar;
  74. var img = document.createElement('img');
  75.  
  76. // Here we register the callback function.
  77. img.onload = function() {
  78. // Code within this function will be executed once the image has loaded.
  79. outerScopeVar = this.width;
  80. };
  81.  
  82. // But, while the image is loading, JavaScript continues executing, and
  83. // processes the following lines of JavaScript.
  84. img.src = 'lolcat.png';
  85. alert(outerScopeVar);
  86.  
  87. var img = document.createElement('img');
  88.  
  89. img.onload = function() {
  90. var localScopeVar = this.width;
  91. alert(localScopeVar);
  92. };
  93.  
  94. img.src = 'lolcat.png';
  95.  
  96. function getWidthOfImage(src) {
  97. var outerScopeVar;
  98.  
  99. var img = document.createElement('img');
  100. img.onload = function() {
  101. outerScopeVar = this.width;
  102. };
  103. img.src = src;
  104. return outerScopeVar;
  105. }
  106.  
  107. var width = getWidthOfImage('lolcat.png');
  108. alert(width);
  109.  
  110. function getWidthOfImage(src, cb) {
  111. var img = document.createElement('img');
  112. img.onload = function() {
  113. cb(this.width);
  114. };
  115. img.src = src;
  116. }
  117.  
  118. getWidthOfImage('lolcat.png', function (width) {
  119. alert(width);
  120. });
  121.  
  122. function getMessage() {
  123. var outerScopeVar;
  124. setTimeout(function() {
  125. outerScopeVar = 'Hello asynchronous world!';
  126. }, 0);
  127. return outerScopeVar;
  128. }
  129. console.log(getMessage());
  130.  
  131. function getMessage(callback) {
  132. setTimeout(function() {
  133. callback('Hello asynchronous world!');
  134. }, 0);
  135. }
  136. getMessage(function(message) {
  137. console.log(message);
  138. });
  139.  
  140. function getMessage() {
  141. return new Promise(function(resolve, reject) {
  142. setTimeout(function() {
  143. resolve('Hello asynchronous world!');
  144. }, 0);
  145. });
  146. }
  147.  
  148. getMessage().then(function(message) {
  149. console.log(message);
  150. });
  151.  
  152. function getMessage() {
  153. var deferred = $.Deferred();
  154. setTimeout(function() {
  155. deferred.resolve('Hello asynchronous world!');
  156. }, 0);
  157. return deferred.promise();
  158. }
  159.  
  160. getMessage().done(function(message) {
  161. console.log(message);
  162. });
  163.  
  164. function getMessage () {
  165. return new Promise(function(resolve, reject) {
  166. setTimeout(function() {
  167. resolve('Hello asynchronous world!');
  168. }, 0);
  169. });
  170. }
  171.  
  172. async function main() {
  173. let message = await getMessage();
  174. console.log(message);
  175. }
  176.  
  177. main();
  178.  
  179. var outerScopeVar; //line 1
  180. $.post('loldog', function(response) { //line 2
  181. outerScopeVar = response;
  182. });
  183. alert(outerScopeVar); //line 3
  184.  
  185. var outerScopeVar; //line 1
  186. $.post('loldog', function(response) { //line 2, takes 10 seconds to complete
  187. outerScopeVar = response;
  188. });
  189. alert("Lets wait for some time here! Waiting is fun"); //line 3
  190. alert(outerScopeVar); //line 4
  191.  
  192. var outerScopeVar;
  193. $.post('loldog', function(response) {
  194. outerScopeVar = response;
  195. alert(outerScopeVar);
  196. });
Add Comment
Please, Sign In to add comment