Skip to content

Instantly share code, notes, and snippets.

@naganowl
Created April 27, 2016 16:15
Show Gist options
  • Save naganowl/5c5b57ee2bc847e8966c37e36801fbc6 to your computer and use it in GitHub Desktop.
Save naganowl/5c5b57ee2bc847e8966c37e36801fbc6 to your computer and use it in GitHub Desktop.
Chaining method calls that are async with assignment can lead to problems

The first result will log undefined for the supposed deferred whereas the second will return the deferred. This is due to the resolution of the callback occurring before the assignment of the referenced variable happens. Note to reproduce this multiple times in console, a new variable will need to be used, otherwise it will seemingly pass because the deferred object already exists.

x = $.Deferred.resolve();
window.foob = $.when(x).done(function(){console.log(1, window.foob)}); console.log('asdf');
window.fooba = $.when(x); fooba.done(function(){console.log(1, window.fooba)}); console.log('asdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment