简体   繁体   English

返回一个空洞的承诺

[英]Return an empty promise

I have a function that returns a jQuery promise. 我有一个返回jQuery承诺的函数。 It looks like this: 它看起来像这样:

addBooks(books: Array<Books>) {
    return $.ajax({
        url: '/Books/AddBooks/',
        type: 'POST',
        data: ko.toJSON(books),
        contentType: 'application/json'
    });
}

I do this so I can reuse this function and chain promise callbacks like: 我这样做,所以我可以重用这个函数和链承诺回调,如:

addBooks.done(() => { alert("Books added!"); })

My question is, what if I want to break out of addBooks early and prevent a trip to the server. 我的问题是,如果我想尽早摆脱addBooks并阻止访问服务器该怎么办? For example: 例如:

addBooks(books: Array<Books>) {

    // An empty array was passed in for some reason.
    // Theres nothing to add so dont try to POST
    if (books <= 0) return null;

    return $.ajax({
        url: '/Books/AddBooks/',
        type: 'POST',
        data: ko.toJSON(books),
        contentType: 'application/json'
    });
}

My example will not compile because my chained done callback example is expecting addBooks to return a promise object, not null. 我的示例将无法编译,因为我的链式完成回调示例期望addBooks返回一个promise对象,而不是null。 How can I return an empty promise (or whatever the correct object is I should return in the situation)? 我怎样才能返回一个空的承诺(或者在这种情况下我应该返回的正确对象)?

How can I return an empty promise (or whatever the correct object is I should return in the situation)? 我怎样才能返回一个空的承诺(或者在这种情况下我应该返回的正确对象)?

Yes, an "empty promise" is appropriate here, if you mean a promise that is already fulfilled with nothing ( undefined , null ). 是的,如果你的意思是一个已经没有实现的承诺( undefinednull ),那么这里的“空承诺”是合适的。

The jQuery syntax to create such is using $.when with a single (or no) argument: 创建这样的jQuery语法是使用带有单个(或无)参数的$.when

if (books <= 0) return $.when(null);

You can return a resolved promise: Instead of 您可以返回已解决的承诺:而不是

if (books <= 0) return null;

use 使用

if (books <= 0) return $.Deferred().resolve();

Beware , though, that jQuery's Promise API does something surprising: Sometimes it calls your done / then /etc. 但要注意 ,jQuery的Promise API做了一些令人惊讶的事情:有时它会调用你的done / then /等等。 callback synchronously with the done / then /etc. done / then / etc同步回调。 call, sometimes it doesn't. 打电话,有时候不打电话。 Most promises libraries ensure that the call is always asynchronous, even if you're calling done / then /etc. 大多数promises库确保调用始终是异步的,即使你正在调用done / then / etc。 on a resolved promise. 在解决的承诺上。 jQuery doesn't, so you get subtle differences. jQuery没有,所以你会得到微妙的差异。

For instance, this code: 例如,这段代码:

addBooks(() => console.log(1));
console.log(2);

...will log ...会记录

2
1

...if you did the ajax call, but ...如果你做了ajax电话,但是

1
2

...if you returned a resolved promise. ......如果你回复了解决的承诺。

From https://api.jquery.com/jquery.when/ 来自https://api.jquery.com/jquery.when/

If you don't pass it any arguments at all, jQuery.when() will return a resolved promise. 如果你根本没有传递任何参数, jQuery.when()将返回一个已解决的promise。

eg 例如

if (books <= 0) return $.when();

and, if you need a value that is not undefined passed on: 并且,如果您需要传递未定义的值:

If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise , it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. 如果将单个参数传递给jQuery.when()并且它不是Deferred或Promise ,则将其视为已解决的Deferred,并且将立即执行任何附加的doneCallbacks。 The doneCallbacks are passed the original argument. doneCallbacks传递原始参数。

eg an empty array is passed on: 例如传递一个空数组:

if (books <= 0) return $.when([]);

eg an empty object is passed on: 例如传递一个空对象:

if (books <= 0) return $.when({});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM