문제

I want, in a certain case, not to query the server with an ajax request but to return an empty array right away. The return type of the function must be a Deferred. (caller is able to chain .then to invocations). I came up with the following:

if(noNeedForAjax) {
    var d = $.Deferred();
    d.resolve([]);
    return d;
}

return $.when( ...

However, it is surprising you cannot do it in one line. I was expecting that $.Deferred().resolve([]) would do the same, but apparently it does not.

Why does not .resolve() return this?

Is it possible to achieve the same in one return statement?

도움이 되었습니까?

해결책

As Andy pointed out, try without return

if(noNeedForAjax) {
    var d = new $.Deferred();
    d.resolve([]);
    d;
};

or, for "do it in one line"

if (noNeedForAjax) {$.when(function() {return []})};

Hope this helps

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top