简体   繁体   English

为什么 forkJoin 对一系列新的 Observable 没有完成?

[英]Why does forkJoin not complete for an array of new Observables?

I'm trying to use forkjoin on some asynchronous tasks but for some reason I don't get the emission on forkjoin.我正在尝试在一些异步任务上使用 forkjoin,但由于某种原因,我没有在 forkjoin 上得到发射。

See stackblitz https://stackblitz.com/edit/angular-12-form-validation-yrstmk?devtoolsheight=33&file=src/app/app.component.ts参见 stackblitz https://stackblitz.com/edit/angular-12-form-validation-yrstmk?devtoolsheight=33&file=src/app/app.component.ts

import { forkJoin, Observable, ReplaySubject } from 'rxjs';
import { switchMap } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  db: ReplaySubject<IDBDatabase> = new ReplaySubject(1);

  constructor() {}

  ngOnInit(): void {
    console.log('init!');

    setTimeout(() => {
      console.log('db init!');
      this.db.next(null);
    }, 2000);

    const test = [this.create(null), this.create(null), this.create(null)];
    forkJoin(test).subscribe(() => {
      console.log('forkjoined!');
    });
  }

  create(variable: any): Observable<any> {
    return this.db.pipe(
      switchMap(
        () =>
          new Observable<any>(observer => {
            setTimeout(() => {
              console.log('next!');
              observer.next(variable);
              observer.complete();
            }, 1000);
          })
      )
    );
  }
}

The answer is the outer observable is a ReplaySubject which will not complete.答案是外部 observable 是一个不会完成的ReplaySubject

I simply added take(1) to my pipe.我只是将take(1)添加到我的管道中。

  create(variable: any): Observable<any> {
    return this.db.pipe(
      take(1),
      switchMap(
        () =>
          new Observable<any>(observer => {
            console.log('next!');
            observer.next(variable);
            observer.complete();
          })
      )
    );
  }

I've just updated your stackblitz.: https://stackblitz.com/edit/angular-12-form-validation-adupub?file=src/app/app.component.ts我刚刚更新了你stackblitz: https://stackblitz.com/edit/angular-12-form-validation-adupub?file=src/app/app.component.ts

You have to:你必须:

  • Add a variable to access it on subscribe(HERE).添加一个变量以在订阅(此处)时访问它。
  • Return a value in the next.在下一个返回值。

Check de diffs:检查差异:
Your code:您的代码:

// line 30
forkJoin(test).subscribe(() => {
   console.log('forkjoined!');
});
//line 42
switchMap(
        () =>
          new Observable<any>(observer => {
            console.log('next!');
            observer.next(variable);
            observer.complete();
          })
      )

My code:我的代码:

//line 30
forkJoin(test).subscribe((data) => {
   console.log('forkjoined!',data);
});
//Line 42
switchMap(
        () =>
          new Observable<any>(observer => {
            console.log('next!');
            observer.next("variable");
            observer.complete();
          })
      )

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM