【问题标题】:Converting old RxJS code to v6 : merge, filter, timer, map, first, toPromise()将旧的 RxJS 代码转换为 v6:merge、filter、timer、map、first、toPromise()
【发布时间】:2020-06-21 17:43:54
【问题描述】:

我正在尝试将此代码更新到版本 6,但我无法弄清楚流程是如何返工的,从阅读和玩耍我想我需要管道结果等,但我看不出如何使用合并流程来完成, filter、timer、map、first、toPromise() 曾经可以工作。有没有 RxJS 人能够教育我或指出我正确的方向?

const chats: chats[] = <chats[]>await Observable.merge(
  this.chatService.$chats.filter(chats => chats.length > 0),
  Observable.timer(5000).map(x => { 
    throw 'Timeout'
  })
).first().toPromise()

if(chats.find( chat => chat.id === chatId )) {
  this.log.log(`Found active chat ${chatId}, navigating to it from push notification...`)                       
}

【问题讨论】:

  • 你需要 await/toPromise() 吗?或者你可以采用完全反应式的 RxJS 方法吗?

标签: rxjs rxjs5 rxjs6 rxjs-observables


【解决方案1】:

试试:

import { merge, timer } from 'rxjs';
import { filter, first, map, } from 'rxjs/operators';

const chats: chats[] = <chats[]> await merge(
  this.chatService.$chat.pipe(
    filter(chats => chats.length > 0),
  ),
  timer(5000).pipe(
    map(x => {
     throw 'Timeout';
   }),
  ),
 ).pipe(
  first(),
 ).toPromise();

 if(chats.find( chat => chat.id === chatId )) {
   this.log.log(`Found active chat ${chatId}, navigating to it from push notification...`)                       
 }

您必须通过管道传递运算符,而不是使用. 链接它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多