Skip to content

jQuery.when: unnecessary asynchronous behavior when called with only one argument #4798

@christian-judt

Description

@christian-judt

Description

The function "jQuery.when" shows apparently unneccessary asynchronous behavior when called with only one argument.

For example executing the code below

const deferred = jQuery.Deferred();
const promise = jQuery.when(deferred);
promise.done(() => console.log("promise done handler called"));
promise.always(() => console.log("promise always handler called"));
console.log("before deferred resolving the promise state is", promise.state());
deferred.resolve();
console.log("after deferred resolving the promise state is", promise.state());

in a javascript console produces the following console output

"before deferred resolving the promise state is" "pending"
"after deferred resolving the promise state is" "pending"
undefined
"promise done handler called"
"promise always handler called"

There really seems to be no reason for the handlers added to the promise returned by the function "jQuery.when" to not be called synchronously while the deferred object (provided as first and only argument to that call of the function "jQuery.when") is being resolved.

When I add any single additional argument (like undefined) to that call of the function "jQuery.when"

const promise = jQuery.when(deferred, undefined);

the unnecessary asynchronous behavior disappears and the handlers are called as synchronously as expected.

Link to test case

I was able to reproduce the behavior using the console at https://codepen.io/mgol/pen/wNWJbZ without any problems, thereby I hope no link to a test case will be necessary here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions