typescript 返回一个空头承诺

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30004503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:59:58  来源:igfitidea点击:

Return an empty promise

javascriptjqueryajaxtypescriptjquery-deferred

提问by Jonathan Eckman

I have a function that returns a jQuery promise. It looks like this:

我有一个返回 jQuery 承诺的函数。它看起来像这样:

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:

我这样做是为了可以重用这个函数和链式 promise 回调,例如:

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. For example:

我的问题是,如果我想提前退出 addBooks 并防止访问服务器怎么办。例如:

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 addBooksto return a promise object, not null. How can I return an empty promise (or whatever the correct object is I should return in the situation)?

我的示例将无法编译,因为我的链式完成回调示例期望addBooks返回一个 promise 对象,而不是 null。我怎样才能返回一个空的承诺(或者在这种情况下我应该返回的任何正确的对象)?

回答by Bergi

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).

是的,如果您的意思是已经没有任何东西实现的承诺 ( undefined, null) ,那么这里使用“空承诺”是合适的。

The jQuery syntax to create such is using $.whenwith a single (or no) argument:

创建此类的 jQuery 语法使用$.when单个(或无)参数:

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

回答by T.J. Crowder

You can return a resolvedpromise: Instead of

您可以返回一个已解决的承诺:而不是

if (books <= 0) return null;

use

利用

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

Beware, though, that jQuery's PromiseAPI does something surprising: Sometimes it calls your done/then/etc. callback synchronously with the 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. on a resolved promise. jQuery doesn't, so you get subtle differences.

但是请注意,jQuery 的PromiseAPI 会做一些令人惊讶的事情:有时它会调用您的done/ then/etc. 与同步回调done/then的/ etc。打电话,有时没有。大多数承诺库确保调用始终是异步的,即使您正在调用done/ then/etc。在已解决的承诺上。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.

...如果你返回了一个已解决的承诺。

回答by Gone Coding

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()将返回一个已解决的承诺。

e.g.

例如

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

and, if you need a value that is not undefined passed on:

并且,如果您需要传递一个非未定义的值:

If a single argumentis 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. The doneCallbacks are passed the original argument.

如果将单个参数传递给 jQuery.when() 并且它不是 Deferred 或 Promise,它将被视为已解决的 Deferred 并且任何附加的 doneCallbacks 将立即执行。doneCallbacks 传递了原始参数。

e.g. an empty array is passed on:

例如传递一个空数组:

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

e.g. an empty object is passed on:

例如传递一个空对象:

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