Angular 5 synchronous HTTP call

前端 未结 3 348
失恋的感觉
失恋的感觉 2021-01-04 03:34

I have an Angular 5 application in which I have to call some heavy REST service (usually takes some seconds). I need its result in different part of application, so I would

3条回答
  •  抹茶落季
    2021-01-04 03:48

    Try using await/async

    async getResult(): Promise {
        if (typeof this.result === 'undefined') 
        {
            // save result
            this.result = await this.service.call()
            .toPromise()
            .then(resp =>resp as MyCustomObject);//Do you own cast here
    
        }
        return this.result;
    }
    

提交回复
热议问题