0
\$\begingroup\$

I got this two subjects that both have a delay and I need the second one to be subscribed when the first one is completed, asynchronously. Both subjects only emit once (http requests) and I'm trying to avoid nested subscriptions but I can't figure out an optimal solution.

What I've got so far: Stackblitz

const order = of('2- created order').pipe(
  delay(100),
  tap(console.log)
);

const google = of('4- logged info to google').pipe(
  delay(500),
  tap(console.log)
);

console.log('1- started creating order');
order
  .pipe(tap(() => google.subscribe()))
  .subscribe(() => console.log("3- didn't wait for google"));

// 1- started creating order
// 2- created order
// 3- didn't wait for google
// 4- logged info to google
\$\endgroup\$
2
  • \$\begingroup\$ This question does not seem to match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. If you happen to fix the code, please provide enough context with it. We can't handle stubs and snippets so well. \$\endgroup\$
    – Mast
    Jun 12 at 20:04
  • \$\begingroup\$ @Mast The code is working fine and the output is just what I intended. I'm just looking for a more slick and RxJS way or some tips on how to improve this. Maybe this was more suited for Stackoverflow, should we migrate it? \$\endgroup\$
    – azerafati
    Jun 13 at 5:08
1
\$\begingroup\$

I think what you want to do is to have 2 subscriptions to your first http request, to achieve that just use a subject and subscribe to it. that way you can handle your second request with operators like switchMap ConcatMap etc, based on your needs. here is the sample based on stack blitz you posted. StackBlitz

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.