异步打字稿函数返回jQuery的承诺 [英] Async TypeScript function return jQuery promise
问题描述
我试图建立一个MVC像打字稿控制器和我有困难,让我的异步方法返回一个延迟的承诺。
I'm trying to build an MVC like controller in TypeScript and I'm having difficulty getting my async method to return a deferred promise.
下面是我的函数签名:
static async GetMatches(input: string, loc?: LatLng):JQueryPromise<any> {
编译器告诉我一个'JQueryPromise'不是一个有效的异步函数的返回类型。
The compiler tells me that a 'JQueryPromise' is not a valid async function return type.
我本来以为这样的事情是最常见的用例异步功能,但我找不到任何的例子。
I would have thought that something like this would be the most common use case for an async function but I can't find any examples.
任何帮助吗?
推荐答案
JQueryPromise不满足通过异步/的await,他们应该inplement以下接口作出的承诺的要求:
JQueryPromise does not satisfy the requirements for the promises made by async/await, they should inplement the following interfaces:
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>;
}
有关详细信息,在这里见第4节承诺:链接
For more details see section 4 Promise here: link
这篇关于异步打字稿函数返回jQuery的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!