jQuery.Deferred().then, how to resolve with multiple parameters
You will have to invoke resolve()
or reject()
in order to pass multiple arguments.
.then()
doesn't include any mechanisms for "spreading" a returned collection. It'll just keep the collection intact as the 1st argument.
But, it will interact with a Deferred
or promise that's returned. From the paragraph starting with "As of jQuery 1.8":
These filter functions can return a new value to be passed along to the promise's .done() or .fail() callbacks, or they can return another observable object (Deferred, Promise, etc) which will pass its resolved / rejected status and values to the promise's callbacks.
So, you could use your example of other()
as the basis for fn()
to keep it fairly succinct with another Deferred()
:
function fn() {
return other().then(function () {
return $.Deferred().resolve(1, 2).promise();
});
}
fn().then(function (a, b) {
console.log(arguments.length, a, b); // 2 1 2
});
http://jsfiddle.net/cqac2/