forkjoin rxjs_如何通过吃披萨来理解RxJS运算符:zip,forkJoin和Combine
forkjoin rxjs
什么是RxJS? (What is RxJS?)
Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change - Wikipedia
响应式编程是一种与数据流和变更传播有关的异步编程范式 -Wikipedia
RxJS is a library for reactive programming using observables that makes it easier to compose asynchronous or callback-based code - RxJS docs
RxJS是用于使用可观察对象进行React式编程的库,可以更轻松地编写异步或基于回调的代码-RxJS文档
The essential concepts in RxJS are
RxJS中的基本概念是
An Observable is a stream of data
可观察的是数据流
Observers can register up to 3 callbacks:
观察者最多可以注册3个回调:
next is called 1:M time to push new values to the observer
下一个称为1:M时间,将新值推送给观察者
error is called at most 1 time when an error occurred
发生错误时最多调用1次错误
complete is called at most 1 time on completion
完成时最多调用1次
Subscription "kicks off" the observable stream
订阅 “开始”可观察的流
Without subscribing the stream won't start emitting values. This is what we call a cold observable.
没有订阅 流将不会开始发出值。 这就是我们所谓的冷 观测。
It's similar to subscribing to a newspaper or magazine... you won't start getting them until you subscribe. Then, it creates a 1 to 1 relationship between the producer (observable) and the consumer (observer).
它的 类似于订阅报纸或杂志...直到订阅后,您才能开始获取它们。 然后,它在生产者(可观察)和消费者(观察者)之间创建一对一的关系。
什么是RxJS运算符? (What are RxJS operators?)
Operators are pure functions that enable a functional programming style of dealing with collections with operations. There are two kinds of operators:
运算符是纯函数,可以使用函数式编程样式来处理带有集合的集合。 运算符有两种:
- Creation operators创作运营商
- Pipeable operators: transformation, filtering, rate limiting, flattening管道运算符:转换,过滤,速率限制,展平
Subjects are a special type of Observable that allows values to be multicast to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. This is what we call a hot observable.
主题是Observable的一种特殊类型,它允许将值多播到许多Observer。 普通的Observable是单播的 (每个订阅的Observer拥有Observable的独立执行),而Subject是多播的。 这就是我们所谓的热点 观察。
In this article, I will focus on the zip
, combineLatest
and forkJoin
operators. These are RxJS combination operators, which means that they enable us to join information from multiple observables. Order, time, and structure of emitted values are the primary differences among them.
在本文中,我将重点介绍zip
, combineLatest
和forkJoin
运算符。 这些是RxJS组合运算符,这意味着它们使我们能够连接来自多个可观察对象的信息。 发射值的顺序,时间和结构是它们之间的主要区别。
Let's look at each one individually.
让我们分别看看每个。
压缩() (zip())
zip
doesn’t start to emit until each inner observable emits at least one value在每个内部可观察到的物体发出至少一个值之前,
zip
才开始发出zip
emits as long as emitted values can be collected from all inner observables只要可以从所有内部观察对象中收集发射的值,
zip
就会发射zip
emits values as an arrayzip
发出值作为数组
Let’s imagine that you are with Mario and Luigi, two of your best friends, at the best Italian restaurant in Rome. Each one of you orders a drink, a pizza, and a dessert. You specify to the waiter to bring the drinks first, then the pizzas, and finally the desserts.
让我们想象一下,您与两个最好的朋友Mario和Luigi在罗马最好的意大利餐厅在一起。 你们每个人都点饮料,比萨饼和甜点。 您可以指定服务员先带饮料,然后带披萨,最后带甜点。
This situation can be represented with 3 different observables, representing the 3 different orders. In this specific situation, the waiter can use the zip
operator to bring (emit) the different order items by category.
这种情况可以用3个不同的观测值表示,分别表示3个不同的阶数。 在这种特定情况下,服务员可以使用zip
运算符按类别携带(发出)不同的订单商品。
❗️❗️❗️Warning❗️❗️❗️
❗️❗️❗️警告❗️❗️❗️
If you go back to the same Italian restaurant with your girlfriend, but she doesn’t want to eat, this is what will happen:
如果您和女友回到同一家意大利餐厅,但她不想吃饭,这将发生以下情况:
If the waiter$
uses the zip
operator, you will only get your drink!
如果waiter$
使用zip
运算符,那么您只会喝酒!
Why?
为什么?
Because, when the waiter$
emits the drinks, the girlfriend$
observable is complete and no more value can be collected from it. Hopefully, the waiter$
can use another operator for us so we don't break up with our girlfriend 😏
因为,当waiter$
发出饮料时,可观察到的girlfriend$
已完成,无法从中收集更多的价值。 希望waiter$
可以为我们使用其他接线员,这样我们就不会与女友分手😏
CombineLatest() (combineLatest())
combineLatest
doesn’t start to emit until each inner observable emits at least one value在每个内部可观察到的
combineLatest
发出至少一个值之前,combineLatest
才开始发出- When any inner observable emits a value, emit the last emitted value from each当任何内部可观察对象发出一个值时,请从每个对象中发出最后一个发出的值
At the exact same restaurant, the smart waiter$
now decide to use combineLatest
operator.
在完全相同的餐厅,聪明的waiter$
现在决定使用combineLatest
运算符。
❗️❗️❗️Warning❗️❗️❗️
❗️❗️❗️警告❗️❗️❗️
With combineLatest
, the order of the provided inner observables does matter.
使用combineLatest
,提供的内部可观察对象的顺序确实很重要。
If you$
is provided first to waiter$
, it will emit only one value ["Tiramisu", "Sprite"]
.
如果首先向waiter$
提供you$
,它将仅发出一个值["Tiramisu", "Sprite"]
。
This is happening because combineLatest
doesn’t start to emit until each inner observable emits at least one value. girlfriend$
starts emitting when the first inner observable emits its last value. Then, combineLatest
emits the last values collected from both inner observables.
之所以发生这种情况,是因为直到每个内部可观察对象发出至少一个值之前, combineLatest
才开始发出。 当第一个内部可观察对象发出其最后一个值时, girlfriend$
开始发出。 然后, combineLatest
发出从两个内部可观察值收集的最后一个值。
forkJoin() (forkJoin())
forkJoin
emits the last emitted value from each inner observables after they all completeforkJoin
后,他们都完成发射来自各内观测最后发出的值forkJoin
will never emit if one of the observables doesn’t complete如果其中一个可观察对象没有完成,
forkJoin
将永远不会发出
When you go to the restaurant and order for a pizza, you don’t want to know all the steps about how the pizza is prepared. If the cheese is added before the tomatoes or the opposite. You just want to get your pizza! This is where forkJoin
comes into play.
当您去餐厅订购披萨时,您不想知道披萨的准备工作的所有步骤。 如果在番茄或相反面之前添加奶酪。 您只想拿披萨! 这是forkJoin
发挥作用的地方。
❗️❗️❗️Warning❗️❗️❗️
W️❗️❗️警告❗️❗️❗️
- If one of the inner observables throws an error, all values are lost如果内部观察值之一抛出错误,则所有值都将丢失
forkJoin
doesn’t completeforkJoin
尚未完成
If you are only concerned when all inner observables complete successfully, you can catch the error from the outside
如果仅在所有内部观测值成功完成时才担心,则可以从外部捕获错误
Then,
forkJoin
completes然后,
forkJoin
完成
- If you don’t care that inner observables complete successfully or not, you must catch errors from every single inner observable如果您不关心内部可观测对象是否成功完成,则必须捕获每个内部可观测对象的错误
Then,
forkJoin
completes然后,
forkJoin
完成
Personally, when I go to a restaurant with friends, I don’t care if one of them receives a burnt pizza. I just want mine 😆 so I will ask the waiter$
to catch the errors from inner observables individually.
就个人而言,当我和朋友一起去餐馆时,我不在乎其中一个是否收到了烧比萨饼。 我只想要我的😆,所以我将要求waiter$
分别从内部可观察对象中捕获错误。
结语 (Wrap up)
We covered a lot in this article! Good examples are important to better understand RxJS operators and how to choose them wisely.
我们在本文中介绍了很多内容! 好的例子对于更好地理解RxJS运算符以及如何明智地选择它们很重要。
For combination operators like zip
, combineLatest
, and forkJoin
the order of inner observables that you provide is also critical, as it can drives you to unexpected behaviours.
对于zip
, combineLatest
和forkJoin
之类的组合运算符,您提供的内部可观察forkJoin
的顺序也很关键,因为它可以使您趋向于意外行为。
There is much more to cover within RxJS and I will do it in further articles.
RxJS包含更多内容,我将在后续文章中介绍。
I hope you enjoyed this article! 😍
希望您喜欢这篇文章! 😍
👉 You can follow me on Twitter to get notified about new Angular/RxJS blog posts and cool tips!
👉您可以在Twitter上关注我,以获取有关Angular / RxJS新博客帖子和酷提示的通知!
翻译自: https://www.freecodecamp.org/news/understand-rxjs-operators-by-eating-a-pizza/
forkjoin rxjs
相关文章:

SQLserver数据库操作帮助类SqlHelper
1 SqlHelper源码 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collections; namespace SQL.Access {/// <summary>/// SqlServer数据访问帮助类/// </summary>public sealed class SqlHelper{#region 私有构造…

python框架之Flask基础篇(一)
一.第一个hello world程序 # codingutf-8 from flask import Flaskapp Flask(__name__)app.route(/) def hello_world():return Hello World!if __name__ __main__:app.run(debugTrue) 1.app参数的设置: 以下几种方式全部拿debug模式举例: .方式一&…

flask部署机器学习_如何开发端到端机器学习项目并使用Flask将其部署到Heroku
flask部署机器学习Theres one question I always get asked regarding Data Science:关于数据科学,我经常被问到一个问题: What is the best way to master Data Science? What will get me hired?掌握数据科学的最佳方法是什么? 什么会雇…

UVALive2678:Subsequence
UVALive2678:Subsequence 题目大意 给定一个数组A和一个整数S。求数组A中,连续且之和不小于S的连续子序列长度最小值。 要求复杂度:Ο(n) Solution 用变量L表示所选区间最左端下标,用变量R表示所选区间最右端下标,用变量sum表示所选区间的和。…

【BZOJ-3712】Fiolki LCA + 倍增 (idea题)
3712: [PA2014]Fiolki Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 303 Solved: 67[Submit][Status][Discuss]Description 化学家吉丽想要配置一种神奇的药水来拯救世界。吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号)。初始时&am…

访问系统相册或调用摄像头
头文件:#import <MobileCoreServices/MobileCoreServices.h> 协议:<UINavigationControllerDelegate, UIImagePickerControllerDelegate> // 调用系统相册获取图片 - (IBAction)getImageFromAlbum:(id)sender {// 判断系统相册是否可用&…

unity镜像_通过镜像学习Unity Multiplayer Basics
unity镜像Unity is one of the most well-known and established engines for game development, and Mirror is a third-party, open source networking solution that allows you to add multiplayer to your games.Unity是最著名的游戏开发引擎之一,而Mirror是第…

java内存模型和线程安全
转载于:https://www.cnblogs.com/Michael2397/p/8397451.html

测试,发布,质量保障,用户体验
1.在实际项目中何时开始设计用户体验:用户的第一印象;从用户的角度考虑问题;软件啊服务始终要记住用户的选择;短期刺激和长期影响 2.测试经验交流:基本名词解释及分类;按测试设计的方法分类;按测…

UIImage存为本地文件与UIImage转换为NSData
UIImage *image"XXX"; //png格式 NSData *imagedataUIImagePNGRepresentation(image); //JEPG格式 //NSData *imagedataUIImageJEPGRepresentation(image); NSArray*pathsNSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString …

如何在JavaScript中实现链接列表
If you are learning data structures, a linked list is one data structure you should know. If you do not really understand it or how it is implemented in JavaScript, this article is here to help you. 如果您正在学习数据结构,则链表是您应该知道的一种…

SVG.path_不连续的线段
1、之前 用<path/>画的 线段等 都是连续的,想知道 是否能画 不连续的线段等 结论:可以 2、测试代码: <?xml version"1.0" encoding"UTF-8"?> <svg width"1000" height"800" viewBo…

Leetcode 之Binary Tree Postorder Traversal(44)
后序遍历,比先序和中序都要复杂。访问一个结点前,需要先判断其右孩子是否被访问过。如果是,则可以访问该结点;否则,需要先处理右子树。 vector<int> postorderTraversal(TreeNode *root){vector<int> resu…

如何创建自己的ESLint配置包
ESLint is a powerful tool that helps you enforce consistent coding conventions and ensure quality in your JavaScript codebase. ESLint是一个功能强大的工具,可帮助您实施一致的编码约定并确保JavaScript代码库的质量。 Coding conventions are sometimes …

MySQL更新命令_UPDATE
创建测试表 mysql> CREATE TABLE product (-> proID int(11) NOT NULL AUTO_INCREMENT COMMENT 商品表主键,-> price decimal(10,2) NOT NULL COMMENT 商品价格,-> type int(11) NOT NULL COMMENT 商品类别(0生鲜,1食品,2生活),-> dtime datetime N…

KVC与KVO
1、键值编码KVC常用的KVC操作方法如下:• 动态设置: setValue:属性值 forKey:属性名(用于简单路径)、setValue:属性值 forKeyPath:属性路径(用于复合路径,例如Person有一个Account类型的属性,…

javaScript 工作必知(三) String .的方法从何而来?
String 我们知道javascript 包括:number,string,boolean,null,undefined 基本类型和Object 类型。 在我的认知中,方法属性应该是对象才可以具有的。 var str"hello,world";var sstr.subString(1,4);//ellalert(typeof…

s3 aws_您需要了解的有关AWS S3的所有信息
s3 awsThis article will provide an in-depth introduction to AWS S3 — the secure, scalable, and super cheap storage service from Amazon Web Services.本文将深入介绍AWS S3-来自Amazon Web Services的安全,可扩展和超便宜的存储服务。 If you have eve…

untitled与前端——初学
“前端” 啥? 百度百科: 就是制作一网页界面。比如360浏览器打开, 包括界面布局设计,搜索框,点击字或图标跳到另一个页面等。 软件Untitled 下载网址:http://www.jetbrains.com/ 下拉 点download࿰…

NSThread
NSThread是轻量级的多线程开发,使用起来也并不复杂,但是使用NSThread需要自己管理线程生命周期。 可以使用对象方法: (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument 直接将操作添加到线程中并…

异步发送邮件、短信、微信
用户创建订单的按钮点击后,服务器存储这个订单信息后,调用发送短信、邮件、微信的接口,发送消息。而发送短信、邮件、微信都要涉及第三方的处理,服务器又要发送一个新的包裹给一个新的服务器,告诉他帮我发一个信息出去…

英语面试简短问题_用简单的英语解释产品设计
英语面试简短问题Product design is the process you go through when you conceptualize and build a product.产品设计是概念化和构建产品时要经历的过程。 The path to building – hardware, software, or even simple prototypes – has different steps and approaches.…

6-12 二叉搜索树的操作集
6-12 二叉搜索树的操作集(30 分) 本题要求实现给定二叉搜索树的5种常用操作。 函数接口定义: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X );…

iOS关于自定义rightBarButtonItem
在常见iOS开发中,我们常遇到这样的需求,如下: 我们需要自定义导航栏右侧按钮,常见的自定义包装按钮如下: //设置rightItem; UIButton *btn [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame CGRectMake(0, 0, 40, 30); btn.selected NO; [btn setTitle:"管理&…

URL里汉字转码
URL里面不能包含中文。 解决办法:进行转码 NSString *urlStr[NSString stringWithFormat:kLotteryBar_putOutReviewUrl,_token,self.reviews_id,_User_Id,reviews_content]; urlStr[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

electron.js_在使用Electron.js之前我希望知道的事情
electron.jsIn this article, Ill share how you can avoid some of the mistakes I made when learning about Electron.js 🤦♂️. I hope it helps!在本文中,我将分享如何避免在学习Electron.js 🤦🤦️时犯的一些错误。 希…

Entity Framework的启动速度优化
最近开发的服务放到IIS上寄宿之后,遇到一些现象,比如刚部署之后,第一次启动很慢;程序放置一会儿,再次请求也会比较慢。比如第一个问题,可以解释为初次请求某一个服务的时候,需要把程序集加载到内…

NSURLConnection的简单使用
遵循代理:NSURLConnectionDataDelegate -(void)fetchWebData:(id)sender{ self.isLoadingYES;NSString *urlStrkRequestUrlStr(self.page);NSURL *url[NSURL URLWithString:urlStr];NSURLRequest *request[NSURLRequest requestWithURL:url];self.connection[N…

tcp reno_如何使用称为Reno Expo的简单入门工具包构建全栈应用程序
tcp renoBuilding any new project from scratch can be intimidating. Theres a lot to decide before you can even start coding to test out your idea.从头开始构建任何新项目都可能令人生畏。 在开始编码以检验您的想法之前,还有很多决定。 How are you buil…

不同命名空间的对象二进制反序列化问题
本质上说,这并不是二进制序列化的问题,甚至不关序列化的问题。 你想要的是在两个内部结构一致但在不同命名空间(甚至不同项目)的同名类间做类型转换。 这个问题很常见,因为实际工作中经常会有此类需求,但是…