Skip to content

Commit e7fdda9

Browse files
committed
Propagete context of returned deferred object in Deferred.then(). Fixes #13160
1 parent c611504 commit e7fdda9

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/deferred.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ jQuery.extend({
3535
newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, [ returned ] );
3636
}
3737
} :
38-
newDefer[ action ]
38+
function() {
39+
newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, arguments );
40+
}
3941
);
4042
});
4143
fns = null;
@@ -72,7 +74,7 @@ jQuery.extend({
7274

7375
// deferred[ resolve | reject | notify ]
7476
deferred[ tuple[0] ] = function() {
75-
deferred[ tuple[0] + "With" ]( promise, arguments );
77+
deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
7678
return this;
7779
};
7880
deferred[ tuple[0] + "With" ] = list.fireWith;

test/unit/deferred.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ test( "jQuery.Deferred.then - deferred (progress)", function() {
273273

274274
test( "jQuery.Deferred.then - context", function() {
275275

276-
expect( 4 );
276+
expect( 7 );
277277

278278
var context = {};
279279

@@ -284,6 +284,12 @@ test( "jQuery.Deferred.then - context", function() {
284284
strictEqual( value, 6, "proper value received" );
285285
});
286286

287+
jQuery.Deferred().resolve().then(function() {
288+
return jQuery.Deferred().resolveWith(context);
289+
}).done(function() {
290+
strictEqual( this, context, "custom context of returned deferred correctly propagated" );
291+
});
292+
287293
var defer = jQuery.Deferred(),
288294
piped = defer.then(function( value ) {
289295
return value * 3;
@@ -295,6 +301,16 @@ test( "jQuery.Deferred.then - context", function() {
295301
strictEqual( this, piped, "default context gets updated to latest promise in the chain" );
296302
strictEqual( value, 6, "proper value received" );
297303
});
304+
305+
var defer2 = jQuery.Deferred(),
306+
piped2 = defer2.then();
307+
308+
defer2.resolve( 2 );
309+
310+
piped2.done(function( value ) {
311+
strictEqual( this, piped2, "default context gets updated to latest promise in the chain (without passing function)" );
312+
strictEqual( value, 2, "proper value received (without passing function)" );
313+
});
298314
});
299315

300316
test( "jQuery.when", function() {

0 commit comments

Comments
 (0)