Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
christian-judt opened this issue Oct 7, 2020 · 2 comments

Comments

@christian-judt
Copy link

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.

@mgol mgol added Ajax Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labels Oct 7, 2020
@mgol
Copy link
Member

mgol commented Oct 7, 2020

Thanks for the report.

The test case is always useful; manually typing code makes the whole process just a bit longer and sometimes this may be enough of a hindrance to postpone looking at the issue. Please prepare a working test case in the future, especially when you already have the code available.

I created https://jsbin.com/quyupuq/1/edit?html,js,console where the behavior difference between single-arg & mutli-arg scenarios is viisble. @gibson042 do you know if this is intended? I was looking at past issues but I haven't found anything that would justify this.

@gibson042
Copy link
Member

This is at the interplay of a few different issues, including #3000 and #3465 (comment) . jQuery.when( singleArg ) is analogous to Promise.resolve( singleArg ), and is expected to "unwrap" thenables and also secondary thenables of the sort that are not possible with native promises (e.g., jQuery.when( jQuery.Deferred().resolve(Promise.resolve(42)) )). But in order to do so, it adopts the asynchrony of then. It might be possible to privilege Deferred objects with synchronous done, but that would seem to introduce its own inconsistency.

At any rate, we would definitely consider a patch that addressed this issue without breaking tests.

@gibson042 gibson042 added Patch Welcome Deferred and removed Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. Ajax labels Oct 19, 2020
@timmywil timmywil added this to the Future milestone May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants