gpt4 book ai didi

javascript - 异步 TypeScript 函数返回 jQuery promise

转载 作者:行者123 更新时间:2023-11-30 08:32:15 25 4
gpt4 key购买 nike

我正在尝试在 TypeScript 中构建类似 MVC 的 Controller ,但我很难让我的异步方法返回延迟 promise 。

这是我的函数签名:

static async GetMatches(input: string, loc?: LatLng):JQueryPromise<any> {

编译器告诉我“JQueryPromise”不是有效的异步函数返回类型。

我原以为像这样的东西是异步函数最常见的用例,但我找不到任何例子。

有什么帮助吗?

最佳答案

来自 the issue detailing async functions (我没有找到更好的引用):

An Async Function must provide a return type annotation that points to a compatible Promise type. Return type inference can only be used if there is a globally defined, compatible Promise type.

然后

Async Functions require a compatible Promise abstraction to operate properly. A compatible implementation implements the following interfaces, which are to be added to the core library declarations (lib.d.ts):

interface IPromiseConstructor<T> {  
new (init: (resolve: (value: T | IPromise<T>) => void, reject: (reason: any) => void) => void): IPromise<T>;
}

interface IPromise<T> {
then<TResult>(onfulfilled: (value: T) => TResult | IPromise<TResult>, onrejected: (reason: any) => TResult | IPromise<TResult>): IPromise<TResult>;
}

jQuery 延迟是 - for good reasons - 不在他们的兼容性列表中。

关于javascript - 异步 TypeScript 函数返回 jQuery promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com