Search code examples
javascriptrxjsrxjs5

How to improve Rxjs statement?


I have the following Rxjs statement:

    const versionsRequest2$ = (num: string) =>
        request2$(num).pipe(
            map((versions) => {
                const p = new P();
                p.setVersions(versions);
                return p;
            }),
        );

How to improve map() statement? I dislike, I have tried to create instance of class and fill it by data from response.

I can improve this using:

map((versions) =>  new P().setVersions(versions)),

If constructor P and setVersions returns this inside


Solution

  • You could write a constructor for P that accepts a versions argument. Then you can just do:

    const versionsRequest2$ = (num: string) =>
      request2$(num).pipe(
        map((versions) => new P(versions)),
      );