当前位置:网站首页>NgRx 里 first 和 take(1) 操作符的区别
NgRx 里 first 和 take(1) 操作符的区别
2022-07-31 23:16:00 【华为云】
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个项目
边栏推荐
- Go1.18 upgrade function - Fuzz test from scratch in Go language
- @JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
- A high-quality WordPress download site template theme developed abroad
- [QNX Hypervisor 2.2 User Manual]9.14 set
- Drawing process of hand-drawn map of scenic spots
- Unity - by casting and cloning method dynamic control under various UGUI create and display
- 基于单片机GSM的防火防盗系统的设计
- 2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
- Learn about C# anonymous methods
- UserAgent resolution
猜你喜欢

Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix

如何减少软件设计和实现之间鸿沟

二叉树非递归遍历

Interview assault 69: TCP reliable?Why is that?

Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)

Binary tree non-recursive traversal

Network security - crack WiFi through handshake packets (detailed tutorial)

基于mysql的消息队列设计

Collation of knowledge points in Ningbo University NBU IT project management final exam

VOT2021比赛简介
随机推荐
[QNX Hypervisor 2.2 User Manual]9.14 set
周总结
Learn about C# anonymous methods
Go1.18 upgrade function - Fuzz test from scratch in Go language
VOT2021 game introduction
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
逐步手撕轮播图3(保姆级教程)
Niuke.com brush questions (1)
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
Collation of knowledge points in Ningbo University NBU IT project management final exam
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
How to reduce the gap between software design and implementation
C程序设计-方法与实践(清华大学出版社)习题解析
The article you worked so hard to write may not be your original
SQL注入 Less47(报错注入) 和Less49(时间盲注)
编译型语言和解释型语言的区别
10大主流3D建模技术
面试题:实现死锁
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
#yyds干货盘点# 面试必刷TOP101:链表中环的入口结点