Angular 5 synchronous HTTP call

前端 未结 3 362
失恋的感觉
失恋的感觉 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 04:05

    The question is how I can wait until HTTP request is finished and then save and return the 're sult' object.

    // Code in service:

    ServiceMethod:Observabke(){
         if (typeof this.result === 'undefined') {
                    return this.service.call().map(res=>res);
    }
    

    // This is code in component.

     componentMethod(){
    
    
            return this.service.ServiceMethod()
                .subscribe(response => {
                    this.result = response;
                   },
                   ()=>//handle error,
                   () => // call completed { return this.result;  // Subscriber});
        }
        return this.weeklyPlayer;  // MyCustomObject (This return won't wait for http to get completed. Not sure why you want multiple returns.)
    
        }
    

    Hope this helps.

提交回复
热议问题