Rxjs await Observable complete - async-await

I’d like to await the completion of my observable. It has a takeUntil I receive a value emited from my cancel$ observable. Its possible that cancel$ emits a valeu befor observable1 even emits its first value. That will give me an error.
await lastValueFrom(observable1.pipe(takeUntil(cancel$)))
console.log("myObservable is completed");
I found that using the deprecated await observable1.toPromise() fixes my problem. But I'd like to avoid something deprecated.

Ok, I finally found my answer here : .toPromise() and lastValueFrom() in rxjs
lastValueFrom takes a second parameter where you can define a default value when nothing has been emitted.

Related

Debug value of Selector

Maybe a simple question but I completely fail to solve it myself.
How to debug/output the value of a selector?
await t
.expect(Selector('.progress-bar.progress-bar-success').getStyleProperty('width')).eql('100%', {timeout: 90000})
I tried to use
console.log(Selector('.progress-bar.progress-bar-success').getStyleProperty('width'));
and also with .value at the end. But I do not get any information.
Is any tip available?
Selector returns a Promise. You need to wait until the promise was resolved. To do this, just add the await keyword before the Selector call.
console.log(await Selector('.progress-bar.progress-bar-success').getStyleProperty('width'));

Get first fulfilled promise in RxJS

Similar to Get first fulfilled promise but in RxJS
If I have two promises A and B, only one of which will succeed, how can I get whichever one fulfills successfully? Some of the promises will have rejection as well. Only the successful promise is needed.
I'm looking for something similar to Promise.race, but which will return only the first promise that fulfills. I'm using promises from RxJS6.
RxJS has race also:
import {race} from 'rxjs'
race(promiseA, promiseB).subscribe(result => console.log('winner is ' + result))

Protractor, do I have to use then() after protractor calls?

Is it ok to write:
getFieldX().clear().sendKeys('abc');
or should I write:
getFieldX().clear().then( () => sendKeys('abc));
I'm totally confused about the Promise handling in protractor. clear() returns a promise, so I should use .then() afterwards, shouldn't I?
But I found examples with .then and some without.
Protractor itself has an example without .then():
https://www.protractortest.org/#/control-flow
Does Protractor has its own mechanism and resolves one after the other promise so there is no need for using .then() after a protractor call that returns a Promise?
And is the control flow of protractor only active in a spec?
When using .sendKeys() in a normal function I have to use .sendKeys().then(...) ?
This all depends on if you are using SELENIUM_PROMISE_MANAGER or not. As this is (has?) becoming deprecated, I would not use it. It should be set to false by default, but if you want to be sure you can add SELENIUM_PROMISE_MANAGER = false; to your conf file. The way that protractor has been moving is to use async/await, so your sendKeys function would look like:
let field = element(by.css('CSS.Selector'));
await field.clear();
await field.sendKeys('abc');
Because these are async functions, you will need to define your function properly, so a basic spec would look like:
describe('Demonstrating async/await',function(){
it('Should send some keys', async function(){
let field = element(by.css('CSS.Selector'));
await field.clear();
await field.sendKeys('abc');
});
});
The important difference there is that a function needs to be defined as async function(). As far as reading the code goes, await simply can be read as "Wait until promise resolved to move on". It does get a bit tedious and you feel like you write await before every line of code (you basically do), but I find it significantly better than .then() trees.

rxjs switch unwrapping observable

I setup a subject and then put some methods on it. It seems to work as intended until it gets to .switch() which I thought would simply keep track of the last call. I get the error Property 'subscribe' does not exist on type 'ApiChange' It seems to convert it to type ApiChange from an observable. I don't understand this behavior. Should I be using a different operator?
Service:
private apiChange = new Subject<ApiChange>();
apiChange$ = this.apiChange.asObservable().distinctUntilChanged().debounceTime(1000).switch();
Component:
this.service.apiChange$.subscribe(change => {
this.service.method(change);
});
.debounceTime(1000) will already assure you will only get a maximum of one value emitted from your observable chain per second. All the values preceding the 1 second quiet time will already be discarded.
With a simple Subject (not a ReplaySubject), past values are not provided to subscribers anyway.
You probably just want to skip the .switch() and enjoy the chain without it.

Transforming Observables in RxJava and RxScala

I have an Observable that emits a List of entries as below:
val obsList: Observable[List[MyEntry] = Observable.from(getListEntry)
// getListEntry would give me a List[MyEntry]
How could I now get the contents piped into another Observable that takes in a MyEntry? I mean, for each entry in the original Observable, I would need to pipe them to another Observable that has a type signature of:
Observable[MyEntry]
I figured out how to do this! I'm using the Monifu library!
So, I would be doing:
Observable.fromIterable(listOfEntry)

Resources