当前位置:网站首页>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个项目
边栏推荐
- Summary of the classic drawing method of histogram
- VOT2021 game introduction
- 一文概述:VPN的基本模型及业务类型
- Payment module implementation
- UserAgent resolution
- 编译型语言和解释型语言的区别
- 21. Support Vector Machine - Introduction to Kernel Functions
- 如何减少软件设计和实现之间鸿沟
- Bionic caterpillar robot source code
- EntityFramework保存到SQLServer 小数精度丢失
猜你喜欢
Golang - from entry to abandonment
21. Support Vector Machine - Introduction to Kernel Functions
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
【Acwing】第62场周赛 题解
IJCAI2022 | 代数和逻辑约束的混合概率推理
How to reduce the gap between software design and implementation
Quick Start Tutorial for flyway
Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
随机推荐
【Acwing】The 62nd Weekly Game Solution
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
Pytest first experience
lua入门案例实战1234定义函数与标准函数库功能
新产品如何进行网络推广?
Flex layout in detail
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
[QNX Hypervisor 2.2 User Manual]9.16 system
@JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
A high-quality WordPress download site template theme developed abroad
嵌入式开发没有激情了,正常吗?
leetcode:126. 单词接龙 II
(26) About menu of the top menu of Blender source code analysis
PHP三元(三目)运算符
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
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
编程语言是什么
"APIO2010" Patrol Problem Solution
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处