2

I have the following method:

  public classMethod(
    payload: Payload,
  ): Observable<Result> {
    const { targetProp } = payload;
    let target;

    return this.secondClass.secondClassMethod({ targetProp }).pipe(
      delayWhen(() =>
        // some other actions
      ),
    );
  }

It is critical to set this target value before calling this.secondClass.secondClassMethod.

Is it possible to call regular async method like this:

  public classMethod(
    req: classMethodRequest,
  ): Observable<classMethodResponse> {
    const { targetProp } = req;
    let target;

    /**
    **  Calling async method here to set ```target```
    ** like 
    ** target = await someAsyncMethod(targetProp)
    **/

    return this.secondClass.secondClassMethod({ targetProp }).pipe(
      delayWhen(() =>
      ),
      
    );
  }

In other words, I would like to invoke method classMethod so, that classMethod will set target variable within it and then it will be possible to use target in the returning construction

return this.secondClass.secondClassMethod({ targetProp }).pipe(
      delayWhen(() =>
      ),
    );

I've tried to cover async method in:

 from(
      (async () => {
        target = await this.someAsyncMethod.setTarget(targetProp);
      })(),
    );

BUT I was told that this cover will invoke in parallel with

return this.secondClass.secondClassMethod({ targetProp }).pipe(
      delayWhen(() =>
      ),
    );

parallel is not an option here :(

0

1 Answer 1

3

You can convert the promise to an observable using from and use switchMap

return from(this.someAsyncMethod.setTarget(targetProp)).pipe(
  switchMap(target => this.secondClass.secondClassMethod({ targetProp }),
  delayWhen(() => ... ),
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works for me. By why just writing async call before return .... will cause that async and code after return will be called in parallel?
It shouldn't if you await the call before return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.