当我将现有代码包装到Deferred中时,whenAllDone Promise/deferred javascript helper中的参数不起作用 [英] Arguments in whenAllDone promise/deferred javascript helper not working when I wrap existing code into a Deferred
问题描述
我正在尝试在TypeScript中实现以下内容:
但是,我的是以 [true | false,currentDeferredArguments]
形式的 flattened 数组.因此,当我循环遍历 $.each(arguments,function(i,args){
)中的参数时,我的args从来都不是数组".第一个 args
只是一个来自 currentDeferred.resolve
调用的 boolean
,然后显然会破坏该循环中的逻辑.
如果我更改代码以仅按他的方式测试代码(如下所示),则代码将按预期工作.
var dfd1 = $ .Deferred();var dfd2 = $ .Deferred();setTimeout(function(){console.log('Resolve dfd2');dfd2.reject({"Options":currentOptions,"ErrorMessage":"Test 2"});},200);setTimeout(function(){console.log('works dfd1');dfd1.reject({"Options":currentOptions,"ErrorMessage":"Test 1"});},100);$ .whenAllDone(dfd1,dfd2).then(
那么如何正确包装对 submitCalculation
的原始调用以正确返回延迟的对象,从而使 whenAllDone
起作用?
问题出在jquery的 when
方法中.
jquery.when:function(下级/*,...,subordinateN */){...
它的行像:
//如果resolveValues仅包含一个Deferred,请使用它.延期=剩余=== 1?下属:jQuery.Deferred(),
这会改变参数的形状,因此我不得不将其恢复为我的代码期望的通用形状(即,当多个deferreds传递给 whenAllDone
时,形状相同)
const deferredArgs = jqueryWhenUsesSubordinate?[[arguments [0],arguments [1]]]:参数$ .each(deferredArgs,function(i,resolveArgs){var target =!resolvedArgs [0]?失败:成功;var data = resolveArgs [1];target.push(data.length === 1?data [0]:data);});
I'm trying to implement the following in TypeScript: https://stackoverflow.com/a/15094263/166231
UPDATE 2 - The problem with original answer is that it doesn't support a single deferred. I modified his fiddle to reproduce the error. http://jsfiddle.net/3h6gwe1x/
UPDATE 1 - Issue is in my usage, so modifying question to indicate that more clearly.
Originally, I had a submitCalculation
method that called .ajax
, so I passed a callback handler in to be called when it was done. This all worked, but I have to prepare for having to run 1..N submitCalculation
calls and wait until all were done. So this was my attempt to simply wrap the original call inside a Deferred.
const p = function() {
const d = $.Deferred();
try {
that.rble.submitCalculation(
currentOptions,
( errorMessage, result ) => {
if ( errorMessage != undefined ) {
d.reject( { "Options": currentOptions, "ErrorMessage": errorMessage } );
}
else {
d.resolve( { "Options": currentOptions, "Result": result } );
}
}
);
} catch (error) {
d.reject( { "Options": currentOptions, "ErrorMessage": "ReSubmit.Pipeline exception: " + error } );
}
return d;
}
$.whenAllDone( p() ).then(
function( ... successes: { Options: KatAppOptions, Result: RBLResult }[] ) { ... },
function( ... failures: { Options: KatAppOptions, ErrorMessage: string }[] ) { ... } );
When the whenAllDone
method is running and hits the following code, the arguments
parameter is not right.
$.when.apply($, deferreds).then(function() {
var failures = [];
var successes = [];
$.each(arguments, function(i, args) {
// If we resolved with `true` as the first parameter
// we have a failure, a success otherwise
var target = args[0] ? failures : successes;
var data = args[1];
// Push either all arguments or the only one
target.push(data.length === 1 ? data[0] : args);
});
if(failures.length) {
return result.reject.apply(result, failures);
}
return result.resolve.apply(result, successes);
});
In the original answer's fiddle, the arguments
was in the form of [ true|false, currentDeferredArguments ][]
. Each element in the array, was the array of parameters resulting from currentDeferred.resolve(true|false, arguments);
.
However, mine is a flattened array in the form of [ true|false, currentDeferredArguments ]
. So when I loop the arguments in $.each(arguments, function (i, args) {
, my args are never an 'array'. The first args
is simply a boolean
from the currentDeferred.resolve
call, which then obviously screws up the logic in that loop.
If I change my code to simply test code the way he did (as in the following) it works as expected.
var dfd1 = $.Deferred();
var dfd2 = $.Deferred();
setTimeout(function () {
console.log('Resolve dfd2');
dfd2.reject( { "Options": currentOptions, "ErrorMessage": "Test 2" } );
}, 200);
setTimeout(function () {
console.log('works dfd1');
dfd1.reject( { "Options": currentOptions, "ErrorMessage": "Test 1" } );
}, 100);
$.whenAllDone( dfd1, dfd2 ).then(
So how can I correctly wrap my original call to submitCalculation
to correctly return a deferred object so whenAllDone
works?
The problem was inside jquery's when
method.
jquery.when: function( subordinate /* , ..., subordinateN */ ) { ...
It has a line like:
// If resolveValues consist of only a single Deferred, just use that.
deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
And this changes the shape of the arguments, so I had to put it back to the common shape my code expects (i.e. the same shape when multiple deferreds are passed to whenAllDone
)
const deferredArgs = jqueryWhenUsesSubordinate
? [[ arguments[ 0 ], arguments[ 1 ] ]]
: arguments
$.each(deferredArgs, function (i, resolvedArgs) {
var target = !resolvedArgs[0] ? failures : successes;
var data = resolvedArgs[1];
target.push(data.length === 1 ? data[0] : data);
});
这篇关于当我将现有代码包装到Deferred中时,whenAllDone Promise/deferred javascript helper中的参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!