Question

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?

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top