Last active
October 25, 2016 22:01
-
-
Save mkropat/8811708 to your computer and use it in GitHub Desktop.
A study in jQuery deferreds, based on Lu4's answer to “jQuery deferreds and promises - .then() vs .done()”.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>jQuery deferreds and promises - .then() vs .done()</title> | |
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-git.css"> | |
</head> | |
<body> | |
<p> | |
A study in jQuery deferreds, based on <em>Lu4's</em> <a | |
href="http://stackoverflow.com/a/15694010/27581">answer to “jQuery | |
deferreds and promises - .then() vs .done()”</a>. | |
</p> | |
<div id="qunit"></div> | |
<div id="qunit-fixture"></div> | |
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script> | |
<script src="http://code.jquery.com/jquery-1.11.0.js"></script> | |
<script type="text/javascript"> | |
asyncTest('then then then', function () { | |
var promise = $.Deferred().resolve('abc'); | |
promise.then(function (x) { | |
equal(x, 'abc'); | |
return 123; | |
}).then(function (x){ | |
equal(x, 123); | |
}).then(function (x){ | |
equal(x, undefined) | |
start(); | |
}); | |
}); | |
asyncTest('done done done', function() { | |
var promise = $.Deferred().resolve('abc'); | |
promise.done(function (x) { | |
equal(x, 'abc'); | |
return 123; | |
}).done(function (x) { | |
equal(x, 'abc'); | |
}).done(function (x) { | |
equal(x, 'abc'); | |
start(); | |
}); | |
}); | |
asyncTest('done then then done done', function () { | |
var promise = $.Deferred().resolve('abc'); | |
promise.done(function (x) { | |
equal(x, 'abc'); | |
return 123; | |
}).then(function (x) { | |
equal(x, 'abc'); | |
}).then(function (x) { | |
equal(x, undefined); | |
return 456; | |
}).done(function (x) { | |
equal(x, 456); | |
}).done(function (x) { | |
equal(x, 456); | |
start(); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for doing this work, I wasn't sure they were different; I think then() is not as safe as done() but offers a nice way to cascade results.