rxjs里使用from operator从一个generator里生成Observable

时间:2022-07-28
本文章向大家介绍rxjs里使用from operator从一个generator里生成Observable,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

源代码:

constructor(private jerryService: JerrySandBoxService,
              private englishGreet: GreetingService,
              @Inject(APP_CONFIG) config: AppConfig) {
    // this.jerryService.print();
    // this.jerryService.print2();
    // console.log(this.englishGreet.greet('Jerry'));
    // map((x: number) => x * x)(of(1, 2, 3)).subscribe((v) => console.log(`value: ${v}`));

    function* generateDoubles(seed) {
      let i = seed;
      while (true) {
        yield i;
        i = 2 * i; // double it
      }
   }
   const iterator = generateDoubles(3);
   const result = from(iterator).pipe(take(12));
    
   result.subscribe(x => console.log('diablo: ' + x));
  }

结果: