0
  this.skusChangeSubscription = this.productForm.get('skus').valueChanges
    .pipe(debounceTime(600)).subscribe(skusValue => {
      console.log('SKUS: ', skusValue);
    ...
    ...
  });

I need code to run this subscribe method directly one time until value changes. It works when value is changed but It doesn't work when subscribed until value is changed. I need this to run one time when subscribed.

canmustu
  • 2,304
  • 4
  • 21
  • 34

2 Answers2

2

Try using startWith operator. It emits the items specified as arguments first before emitting from the source observable.

this.skusChangeSubscription = this.productForm.get('skus').valueChanges
  .pipe(
    startWith(this.productForm.get('skus').value),
    debounceTime(600)
  )
  .subscribe(skusValue => {
    console.log('SKUS: ', skusValue);
  ...
  ...
  });
ruth
  • 29,535
  • 4
  • 30
  • 57
  • It works by changing `0` with `this.productForm.get('skus').value`. Is there any way not to specify first value passing to `startWith` function parameter? – canmustu Mar 19 '20 at 11:20
  • Thank you so much by the way, it works. `getValue()` doesn't exist on my intellisense. I think I'm using version of angular or typescript that is not updated with `getValue()`. – canmustu Mar 19 '20 at 11:25
  • Right, I've got it the other way around. `getValue()` was removed and it was updated with `.value`. See [here](https://stackoverflow.com/q/38784566/6513921). But `.value` was also removed as of Rxjs V 6.5.0. See [here](https://github.com/ReactiveX/rxjs/issues/5085#issuecomment-542716708). So it will work while it lasts, but it might not be available if you plan to upgrade in the future. – ruth Mar 19 '20 at 11:30
1

Try this:

this.skusChangeSubscription = this.productForm.get('skus')
    .pipe(take(1)).subscribe(skusValue => {
      console.log('SKUS: ', skusValue);
    ...
    ...
  });

Pauho
  • 74
  • 4
  • If you want to take just the first value that arrives from your subscripcion you can use the rxjs operator take. To understand it, try by yourself here [link](https://stackblitz.com/edit/rxjs-biyohu?devtoolsheight=60) and here it is the documentation [link](https://rxjs-dev.firebaseapp.com/api/operators/take) – Pauho Mar 19 '20 at 11:19