在typescript中等待异步函数

在typescript中等待异步函数,typescript,es6-promise,Typescript,Es6 Promise,我最近加入了Angular2,我来自c#背景。我目前正在客户机中进行“数据服务层”的工作,当然,我正在以c#项目中的方式进行这项工作。。。(因此,如果我做错了什么,也请告诉我) 我有一个这样的结构: 数据服务 DataService(将响应从WS映射到指定类型T的通用DataService)。这包括get、post和put函数 博客服务 BlogService扩展了DataService,这将是例如BlogListComponent将使用的服务 我还将使用rgnx商店,但我暂时不考虑这个问题 现

我最近加入了Angular2,我来自c#背景。我目前正在客户机中进行“数据服务层”的工作,当然,我正在以c#项目中的方式进行这项工作。。。(因此,如果我做错了什么,也请告诉我)

我有一个这样的结构:

  • 数据服务 DataService(将响应从WS映射到指定类型T的通用DataService)。这包括get、post和put函数

  • 博客服务 BlogService扩展了DataService,这将是例如BlogListComponent将使用的服务

  • 我还将使用rgnx商店,但我暂时不考虑这个问题

    现在是代码

    这是来自数据服务的

    protected post<T>(endpointUrl: string, request: object) : Promise<T> {
            return this.processResponse<T>(this.http.post(endpointUrl, JSON.stringify(request), this.options()));
        }
    
    protected async processResponse<T>(response : Observable<Response>) : Promise<T> {
            return response.map(response => this.extractJson(response))
                .toPromise()
                .catch(this.handleError);
        }
    
    以前被踩过

    b => blogPosts = b
    
    所以我只返回一个空数组

    尝试2:这是async的本质,因此我会告诉函数使用wait关键字等待承诺返回

    await super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
    await super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
    wait super.post(BlogDataService.GET\u BLOGPOSTS\u端点,请求),然后(b=>BLOGPOSTS=b);
    
    然后编译器给出了一个巨大的错误,说我不能在异步函数中使用blogpost数组作为返回类型

    var s = () => super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
    async function awaiter(asyncFunction) : Promise<any> {
        await asyncFunction;
    }
    
    尝试3:然后我删除了GetBlogPosts的异步声明,因为我确实想返回一个BlogPost数组,而不是将其更改为任何数组,并添加了一个嵌套函数,它是一个异步函数

    var s = () => super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
    async function awaiter(asyncFunction) : Promise<any> {
        await asyncFunction;
    }
    
    var s=()=>super.post(BlogDataService.GET\u BLOGPOSTS\u端点,请求),然后(b=>BLOGPOSTS=b);
    异步函数等待器(asyncFunction):承诺{
    等待异步函数;
    }
    
    这和代码看起来一样好——一点也不!虽然我可以看到GetBlogPosts在异步函数返回任何内容之前没有命中它的return语句,所以我想我离它越来越近了

    无论如何,我很可能在这里做了一些根本错误的事情。我希望我能听到你们是如何实现类似的东西的

    感谢一位沮丧的新手

    尝试2:这是async的本质,因此我会告诉函数使用wait关键字等待承诺返回

    await super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
    await super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
  • 这可能是问题,也可能不是问题,这取决于
    This.handleError
    的作用。如果它返回一个被拒绝的承诺,请跳到下面的#2。如果不是:通常情况下,通过承诺,您要么返回链,要么处理错误,但不能两者都返回。如果你同时做这两件事,重要的是确保你不会把拒绝变成不恰当的解决方案。请记住,
    then
    catch
    处理程序创建了一个新的承诺,该承诺将根据他们的行为得到解决/拒绝

  • 由于您使用的是
    async
    函数,因此使用promise回调并不惯用。从声明中删除
    async
    ,或者使用
    wait
    (可能还有
    try
    /
    catch

  • 如果保持
    异步
    ,那么这样做可能更惯用:

    protected async processResponse<T>(response : Observable<Response>) : Promise<T> {
        return await response.map(response => this.extractJson(response)).toPromise();
    }
    
    受保护的异步processResponse(响应:可观察):承诺{
    return wait response.map(response=>this.extractJson(response)).toPromise();
    }
    
    …我们再次将错误处理留给调用方

    尝试2:这是async的本质,因此我会告诉函数使用wait关键字等待承诺返回

    await super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
    await super.post<Array<BlogPost>>(BlogDataService.GET_BLOGPOSTS_ENDPOINT, request).then(b => blogPosts = b);
    
  • 这可能是问题,也可能不是问题,这取决于
    This.handleError
    的作用。如果它返回一个被拒绝的承诺,请跳到下面的#2。如果不是:通常情况下,通过承诺,您要么返回链,要么处理错误,但不能两者都返回。如果你同时做这两件事,重要的是确保你不会把拒绝变成不恰当的解决方案。请记住,
    then
    catch
    处理程序创建了一个新的承诺,该承诺将根据他们的行为得到解决/拒绝

  • 由于您使用的是
    async
    函数,因此使用promise回调并不惯用。从声明中删除
    async
    ,或者使用
    wait
    (可能还有
    try
    /
    catch

  • 如果保持
    异步
    ,那么这样做可能更惯用:

    protected async processResponse<T>(response : Observable<Response>) : Promise<T> {
        return await response.map(response => this.extractJson(response)).toPromise();
    }
    
    受保护的异步processResponse(响应:可观察):承诺{
    return wait response.map(response=>this.extractJson(response)).toPromise();
    }
    
    …我们再次将错误处理留给调用方