1

Quote from RxJS dev team:

To fix all these issues, we decided to deprecate toPromise(), and to introduce the two new helper functions for conversion to Promises.

Use one of the two new functions

As a replacement to the deprecated toPromise() method, you should use one of the two built in static conversion functions firstValueFrom or lastValueFrom....

In my case I send a get request to the server to check if the server is available or not. The main function (in this case the ngOnInit()) will not go further until an HTTP response or an error comes back.

At this part of the article, they suggest adding a timeout to the lastValueFrom() function, which should be added as a config config?: LastValueFromConfig<D>.

My code:

    let something = lastValueFrom(this.http.get<resultDTO> 
    ('/api/account/test'),).then(
          res => {
            this.note = (res.data);
          }
    );

How do I have to set this config and pass it to the function?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Matthew
  • 93
  • 2
  • 7
  • 1
    _Should_ it be added as a config? Those docs link to a separate pipeable operator, https://rxjs.dev/api/operators/timeout. As described in [the docs](https://rxjs.dev/api/index/function/lastValueFrom) and seen in [the types](https://github.com/ReactiveX/rxjs/blob/bb3da37fd3f862d09f3c139f647acd694a4c4877/src/internal/lastValueFrom.ts#L4-L6), the config is only for a default value. – jonrsharpe Nov 22 '21 at 14:02

1 Answers1

2

The timeout operator must be added to the HTTP request and not the Promise from lastValueFrom.

let something = lastValueFrom(
  this.http.get<resultDTO>('/api/account/test').pipe(
    timeout(5000)      // <-- HTTP request will error out if no response for 5 seconds
  )
).then(res => {
  this.note = (res.data);
});

The LastValueFromConfig argument at the moment (RxJS v7) has only one value.

export interface LastValueFromConfig<T> {
  defaultValue: T;
}

This has nothing to do with the timeout behaviour of the observable.

So in your case you could do

let something = lastValueFrom(
  this.http.get<resultDTO>('/api/account/test').pipe(
    timeout(5000)      // <-- HTTP request will error out if no response for 5 seconds
  ),
  { defaultValue: { data: ''} }  // <-- default value to be used
).then(res => {
  this.note = (res.data);
});

Being said that, this is one of those cases where I'd say there is no inherent need for converting the Observable to a Promise. You could simply make do only with the observable

this.http.get<resultDTO>('/api/account/test').pipe(
  timeout(5000)      // <-- HTTP request will error out if no response for 5 seconds
).subscribe({
  next: (res: resultDTO) => {
    this.note = res.data;
  },
  error: (error: any) => {
    // handle errors
  }
});
ruth
  • 29,535
  • 4
  • 30
  • 57
  • 1
    Then what about this: [LastValueFrom()](https://rxjs.dev/api/index/function/lastValueFrom#lastvaluefrom) It exactly says: An optional configuration object to define the defaultValue to use if the source completes without emitting a value – Matthew Nov 23 '21 at 04:16
  • 1
    I even see it in my source code in VSCode [https://s21.picofile.com/file/8444203200/2021_11_23_07_40_25_%E2%97%8F_test_component_ts_Doctors_Visual_Studio_Code.png](https://s21.picofile.com/file/8444203200/2021_11_23_07_40_25_%E2%97%8F_test_component_ts_Doctors_Visual_Studio_Code.png) I just couldn't instantiate a LastValueFromConfig. – Matthew Nov 23 '21 at 04:22
  • @Matthew: You aren't providing any `LastValueFromConfig` at the moment. See the statement marked as `<-- default value to be used` in the 2nd section of my answer. Also the parameter `config?: LastValueFromConfig` with a question mark `?` means that the `config` is optional and could be ignored if you don't need it. – ruth Nov 23 '21 at 07:23