异步 TypeScript 函数 return jQuery promise
Async TypeScript function return jQuery promise
我正在尝试在 TypeScript 中构建一个类似 MVC 的控制器,但我很难让我的异步方法成为 return 延迟承诺。
这是我的函数签名:
static async GetMatches(input: string, loc?: LatLng):JQueryPromise<any> {
编译器告诉我 'JQueryPromise' 不是有效的异步函数 return 类型。
我原以为像这样的东西是异步函数最常见的用例,但我找不到任何例子。
有什么帮助吗?
JQueryPromise 不满足 async/await 做出的承诺的要求,他们应该实现以下接口:
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 节承诺:link
来自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 - 不在他们的兼容性列表中。
要为 jQuery 承诺启用 async/await,请使用以下命令。它与来自 DefinitelyTyped 的 jquery.d.ts 结合使用。
class JQueryPromise<T> {
constructor(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) {
let dfd = $.Deferred<T>();
function fulfilled(value?: T | PromiseLike<T>) {
let promise = <PromiseLike<T>>value;
if (value && promise.then) {
promise.then(fulfilled, rejected);
}
else {
dfd.resolve(<T>value);
}
}
function rejected(reason) {
let promise = <PromiseLike<T>>reason;
if (reason && promise.then) {
promise.then(fulfilled, rejected);
}
else {
dfd.reject(<T>reason);
}
}
executor(fulfilled, rejected);
return dfd.promise();
}
}
示例:
interface IData {
value: number;
}
async function getValue(): JQueryPromise<number> {
let result = <IData> await $.getJSON('/data.json');
return result.value;
}
我正在尝试在 TypeScript 中构建一个类似 MVC 的控制器,但我很难让我的异步方法成为 return 延迟承诺。
这是我的函数签名:
static async GetMatches(input: string, loc?: LatLng):JQueryPromise<any> {
编译器告诉我 'JQueryPromise' 不是有效的异步函数 return 类型。
我原以为像这样的东西是异步函数最常见的用例,但我找不到任何例子。
有什么帮助吗?
JQueryPromise 不满足 async/await 做出的承诺的要求,他们应该实现以下接口:
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 节承诺:link
来自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, compatiblePromise
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 - 不在他们的兼容性列表中。
要为 jQuery 承诺启用 async/await,请使用以下命令。它与来自 DefinitelyTyped 的 jquery.d.ts 结合使用。
class JQueryPromise<T> {
constructor(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) {
let dfd = $.Deferred<T>();
function fulfilled(value?: T | PromiseLike<T>) {
let promise = <PromiseLike<T>>value;
if (value && promise.then) {
promise.then(fulfilled, rejected);
}
else {
dfd.resolve(<T>value);
}
}
function rejected(reason) {
let promise = <PromiseLike<T>>reason;
if (reason && promise.then) {
promise.then(fulfilled, rejected);
}
else {
dfd.reject(<T>reason);
}
}
executor(fulfilled, rejected);
return dfd.promise();
}
}
示例:
interface IData {
value: number;
}
async function getValue(): JQueryPromise<number> {
let result = <IData> await $.getJSON('/data.json');
return result.value;
}