You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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.
Description
The function "jQuery.when" shows apparently unneccessary asynchronous behavior when called with only one argument.
For example executing the code below
in a javascript console produces the following console output
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"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.
The text was updated successfully, but these errors were encountered: