Async TypeScript function return jQuery promise

天涯浪子 提交于 2020-01-05 12:16:13

问题


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.

Here's my function signature:

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

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.

Any help?


回答1:


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>;  
}

For more details see section 4 Promise here: link




回答2:


From the issue detailing async functions (I found no better reference):

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.

and then

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 deferreds are - for good reasons - not on their compatibility list.




回答3:


To enable async/await for jQuery promises, use the following. It works in combination with jquery.d.ts from DefinitelyTyped.

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();
    }
}

Example:

interface IData {
  value: number;
}

async function getValue(): JQueryPromise<number> {
    let result = <IData> await $.getJSON('/data.json');
    return result.value;
}


来源:https://stackoverflow.com/questions/35873617/async-typescript-function-return-jquery-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!