Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active October 25, 2016 22:01
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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()”.
<!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>
@ca0v
Copy link

ca0v commented Nov 14, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment