当前位置:网站首页>Difference between first and take(1) operators in NgRx
Difference between first and take(1) operators in NgRx
2022-07-31 23:22:00 【HUAWEI CLOUD】
first() 运算符采用可选的 predicate 函数,并在源完成后没有匹配的值时发出错误通知.
下列代码会报错:
import { EMPTY, range } from 'rxjs';import { first, take } from 'rxjs/operators';EMPTY.pipe(first()).subscribe(console.log, err => console.log('Jerry Error:', err));
同理,下面代码也会报错:
range(1, 5).pipe( first(val => val > 6),).subscribe(console.log, err => console.log('Error', err));
下列代码输出1:
import { EMPTY, range } from 'rxjs';import { first, take } from 'rxjs/operators';range(1, 5) .pipe(first()) .subscribe(console.log, err => console.log('Error', err));
另一方面, take(1) 只取第一个值并完成.不涉及进一步的逻辑.
import { EMPTY, range } from 'rxjs';import { first, take } from 'rxjs/operators';EMPTY.pipe( take(1),).subscribe(console.log, err => console.log('Error', err));上面代码不会有任何输出:

使用 first 操作符需谨慎,当满足下列条件使,可以使用 first:
(1)您将发出的零项视为错误条件(例如,在发出之前完成)并且如果出现错误的可能性大于 0%,则您可以优雅地处理它
(2)或者你 100% 知道源 observable 会发出至少1个项目
边栏推荐
猜你喜欢
Mysql environment installation under Linux (centos)

Flink 1.13(八)CDC

(26) About menu of the top menu of Blender source code analysis

逐步手撕轮播图3(保姆级教程)

IJCAI2022 | 代数和逻辑约束的混合概率推理

What is customer profile management?

UOS统信系统 - WindTerm使用
I don't know what to do with sync issues

消息队列消息存储设计(架构实战营 模块八作业)

A high-quality WordPress download site template theme developed abroad
随机推荐
2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
Binary tree non-recursive traversal
编写方法将一个数组扁平化并且去重和递增排序
输入输出优化
SQL injection Less42 (POST type stack injection)
C# Rectangle基本用法和图片切割
硬件设备计算存储及数据交互杂谈
信息学奥赛一本通 1941:【07NOIP普及组】Hanoi双塔问题 | 洛谷 P1096 [NOIP2007 普及组] Hanoi 双塔问题
Judging decimal points and rounding of decimal operations in Golang
UOS统信系统 - WindTerm使用
博弈论(Depu)与孙子兵法(42/100)
A high-quality WordPress download site template theme developed abroad
一文概述:VPN的基本模型及业务类型
【ACM】2022.7.31训练赛
Difference Between Stateless and Stateful
SQL注入 Less47(报错注入) 和Less49(时间盲注)
#yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
Input and output optimization
网易云信圈组上线实时互动频道,「破冰」弱关系社交