question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Await .onFirstEmitted() and .onEmitted(n); of observable values and then continue

See original GitHub issue

First off, huge fan of observer-spy! What a life-saver!

Is your feature request related to a problem? Please describe. Comparable to .onComplete() and .onError(), it would be very useful to have something that awaits the first or n value emitted by an observable before continuing. This is because not all observables should be completing and need to stay alive. Observable Store is a good example of this where I would like a (Vue) component to keep being updated when ever the store is updated, which means I don’t want to complete the observable.

Describe the solution you’d like

Something like this maybe:

const test$ = of([0,1,2,3,4,5,6,7,8,9,10]);
const _test= subscribeSpyTo(test$);

await _test.onFirstEmitted();
expect(_test.getFirstValue()).toEqual(0);

await _test.onEmitted(5);
expect(_test.getValues()).toEqual([0,1,2,3,4]);

Describe alternatives you’ve considered

Continuing from the example above:

const test$ = of([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const testTakeOne$ = test$.pipe(take(1));
const _testTakeOne = subscribeSpyTo(testTakeOne$);

await _testTakeOne.onComplete();
expect(_testTakeOne.getFirstValue()).toEqual(0);

const testTakeFive$ = test$.pipe(take(5));
const _testTakeFive = subscribeSpyTo(testTakeFive$);

await _testTakeFive.onComplete();
expect(_testTakeFive.getValues()).toEqual([0, 1, 2, 3, 4]);

Additional context So before making this post I thought this couldn’t be done but by rubber ducking I realized that it could be done, albeit in a convoluted way. If nothing else, I could most likely write an extension method for it.

Thanks for reading and please like and subscribe if you want more feature requests!

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JasonLandbridgecommented, Nov 21, 2022

Haha I will keep it in mind, thanks for the consideration!

1reaction
shairezcommented, Nov 20, 2022

Thanks @JasonLandbridge ! lol, I subscribed 😃

I like the idea I think I’d change onEmitted to onCount or onValuesCount to make it more clear… because onEmitted might also be referring to the values themselves (like when the number 5 arrives, stop)

Mind creating a PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I `await` on an Rx Observable? - Stack Overflow
You have to pass a promise to await . Convert the observable's next event to a promise and await that. if (condition) {...
Read more >
RxJS Observable interop with Promises and Async-Await
That means that if the Observable emits the value “hi” then waits 10 seconds before it completes, the returned promise will wait 10...
Read more >
Angular: Observables, async/await, and Promises, oh my!
When trying to pick up Angular, this was super frustrating and my gut reaction was to use the very-handy toPromise() method that Observables...
Read more >
Simplifying Angular with Async/Await - Beyond Java
This is a long article (once again!), so let's summarize it quickly. In a nutshell, an Observable is a Promise with multiple values....
Read more >
rxjs-for-await/README.md at master - GitHub
This strategy will yield every single value the observable source emits, one at a time, until the observable completes or errors. Pros. All...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found