Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var outerScopeVar;
- var img = document.createElement('img');
- img.onload = function() {
- outerScopeVar = this.width;
- };
- img.src = 'lolcat.png';
- alert(outerScopeVar);
- var outerScopeVar;
- setTimeout(function() {
- outerScopeVar = 'Hello Asynchronous World!';
- }, 0);
- alert(outerScopeVar);
- // Example using some jQuery
- var outerScopeVar;
- $.post('loldog', function(response) {
- outerScopeVar = response;
- });
- alert(outerScopeVar);
- // Node.js example
- var outerScopeVar;
- fs.readFile('./catdog.html', function(err, data) {
- outerScopeVar = data;
- });
- console.log(outerScopeVar);
- // with promises
- var outerScopeVar;
- myPromise.then(function (response) {
- outerScopeVar = response;
- });
- console.log(outerScopeVar);
- // geolocation API
- var outerScopeVar;
- navigator.geolocation.getCurrentPosition(function (pos) {
- outerScopeVar = pos;
- });
- console.log(outerScopeVar);
- var outerScopeVar;
- helloCatAsync();
- alert(outerScopeVar);
- function helloCatAsync() {
- setTimeout(function() {
- outerScopeVar = 'Nya';
- }, Math.random() * 2000);
- }
- // 1. Call helloCatAsync passing a callback function,
- // which will be called receiving the result from the async operation
- helloCatAsync(function(result) {
- // 5. Received the result from the async function,
- // now do whatever you want with it:
- alert(result);
- });
- // 2. The "callback" parameter is a reference to the function which
- // was passed as argument from the helloCatAsync call
- function helloCatAsync(callback) {
- // 3. Start async operation:
- setTimeout(function() {
- // 4. Finished async operation,
- // call the callback passing the result as argument
- callback('Nya');
- }, Math.random() * 2000);
- }
- var outerScopeVar;
- var img = document.createElement('img');
- // Here we register the callback function.
- img.onload = function() {
- // Code within this function will be executed once the image has loaded.
- outerScopeVar = this.width;
- };
- // But, while the image is loading, JavaScript continues executing, and
- // processes the following lines of JavaScript.
- img.src = 'lolcat.png';
- alert(outerScopeVar);
- var img = document.createElement('img');
- img.onload = function() {
- var localScopeVar = this.width;
- alert(localScopeVar);
- };
- img.src = 'lolcat.png';
- function getWidthOfImage(src) {
- var outerScopeVar;
- var img = document.createElement('img');
- img.onload = function() {
- outerScopeVar = this.width;
- };
- img.src = src;
- return outerScopeVar;
- }
- var width = getWidthOfImage('lolcat.png');
- alert(width);
- function getWidthOfImage(src, cb) {
- var img = document.createElement('img');
- img.onload = function() {
- cb(this.width);
- };
- img.src = src;
- }
- getWidthOfImage('lolcat.png', function (width) {
- alert(width);
- });
- function getMessage() {
- var outerScopeVar;
- setTimeout(function() {
- outerScopeVar = 'Hello asynchronous world!';
- }, 0);
- return outerScopeVar;
- }
- console.log(getMessage());
- function getMessage(callback) {
- setTimeout(function() {
- callback('Hello asynchronous world!');
- }, 0);
- }
- getMessage(function(message) {
- console.log(message);
- });
- function getMessage() {
- return new Promise(function(resolve, reject) {
- setTimeout(function() {
- resolve('Hello asynchronous world!');
- }, 0);
- });
- }
- getMessage().then(function(message) {
- console.log(message);
- });
- function getMessage() {
- var deferred = $.Deferred();
- setTimeout(function() {
- deferred.resolve('Hello asynchronous world!');
- }, 0);
- return deferred.promise();
- }
- getMessage().done(function(message) {
- console.log(message);
- });
- function getMessage () {
- return new Promise(function(resolve, reject) {
- setTimeout(function() {
- resolve('Hello asynchronous world!');
- }, 0);
- });
- }
- async function main() {
- let message = await getMessage();
- console.log(message);
- }
- main();
- var outerScopeVar; //line 1
- $.post('loldog', function(response) { //line 2
- outerScopeVar = response;
- });
- alert(outerScopeVar); //line 3
- var outerScopeVar; //line 1
- $.post('loldog', function(response) { //line 2, takes 10 seconds to complete
- outerScopeVar = response;
- });
- alert("Lets wait for some time here! Waiting is fun"); //line 3
- alert(outerScopeVar); //line 4
- var outerScopeVar;
- $.post('loldog', function(response) {
- outerScopeVar = response;
- alert(outerScopeVar);
- });
Add Comment
Please, Sign In to add comment