当前位置:网站首页>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个项目
边栏推荐
- Flink 1.13(八)CDC
- 博弈论(Depu)与孙子兵法(42/100)
- Document management and tools in the development process
- 基于simulink的Passive anti-islanding-UVP/OVP and UFP/OFP被动反孤岛模型仿真
- Collation of knowledge points in Ningbo University NBU IT project management final exam
- 一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
- SQL injection Less42 (POST type stack injection)
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
- 网络安全--通过握手包破解WiFi(详细教程)
- @JsonFormat(pattern="yyyy-MM-dd") time difference problem
猜你喜欢

A high-quality WordPress download site template theme developed abroad

C程序设计-方法与实践(清华大学出版社)习题解析

Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios

【ACM】2022.7.31训练赛

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

网络安全--通过握手包破解WiFi(详细教程)

Audio alignment using cross-correlation

Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决

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#

Quick Start Tutorial for flyway
随机推荐
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#
SQL注入 Less38(堆叠注入)
Write a database document management tool based on WPF repeating the wheel (1)
游戏安全03:缓冲区溢出攻击简单解释
IDA PRO中汇编结构体识别
[QNX Hypervisor 2.2用户手册]9.15 suppress
SQL injection Less47 (error injection) and Less49 (time blind injection)
浏览器下载快捷方式到桌面(PWA)
How to debug TestCafe
Several methods for deleting specified elements in Golang slices
uniapp小程序检查、提示更新
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
Go1.18 upgrade function - Fuzz test from scratch in Go language
Mysql environment installation under Linux (centos)
A high-quality WordPress download site template theme developed abroad
[NLP] What is the memory of the model!
编译型语言和解释型语言的区别
HTC使用官方固件作为底包制作rom卡刷包教程
Interview assault 69: TCP reliable?Why is that?
hboot and recovery, boot.img, system.img