惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - 草珊瑚

前端git开发分支各种场景管理 RxJS Subject学习 微信小程序登陆流程(20200322) vue依赖收集的策略 eggjs2.x版本异步获取config配置方案 - 草珊瑚 - 博客园 dubbo连接过程 计算机中对流的理解 Egg.js运行环境配置场景 eggjs异常捕获机制 极客时间数据结构与算法之美笔记7 JS项目快速压缩(windows平台) - 草珊瑚 - 博客园 JS项目快速压缩(windows平台) - 草珊瑚 - 博客园 Maven工程的POM继承 Docker构建一个node镜像 win10家庭版安装Docker for Windows linux,vim和bash命令小册 vue文档阅读笔记——计算属性和侦听器 nodejs的jekins部署 `vue-router`的`History`模式下的项目发布
Promise和Observable的映射
草珊瑚 · 2019-09-13 · via 博客园 - 草珊瑚

前言

  1. promise解决了嵌套地狱的问题,Observable解决了promise只有一个结果,和不可以取消的问题。
  2. 使用的是rxjs6版本。
  3. 这篇文章是方便使用Observable的API替换Promise的API。

正常用法

promise
.then(result => {})
.catch(error => {})
.finally(() => {});

observable.subscribe(
  result => {},
  error => {},
  ()=>{},  // finally
);

then

promise
.then(result => {})
.then(result => {})
.then(result => {})

import { concat } from 'rxjs';
concat(observable0,observable1,observable2).subscribe(
  result => {},
  error => {},
  ()=>{},  // finally
);

// promise
this.getOne().then(data => {
  // 这里返回另外一个Promise
  return this.getTwo(data);
}).then(data => {
  console.log(data);  // 这里打印第二个Promise的值
  return this.getThree(data);
}).then(data => {
  console.log(data); // 这里打印第三个Promise的值
});

// Observable
import { forkJoin, Observable,from,pipe } from 'rxjs';
import { retryWhen, map, mergeMap } from 'rxjs/operators';

from(this.getOne)
.pipe(
    mergeMap(oneData => {
        console.log(oneData)
        return from(this.getTwo)
    }),
    mergeMap(twoData => {
        console.log(twoData)
        return from(this.getThree)
    })
)
.subscribe(threeData => {
    console.log(threeData)
    ...
})

Promise.all

Promise.all([promise0, promise1]).then((result)=>{});

import { forkJoin } from 'rxjs';
forkJoin([observable0, observable1]).subscribe(result => {});

Promise.race

Promise.race([promise0, promise1]).then((result)=>{});

import { race } from 'rxjs/observable/race';
race([observable0, observable1]).subscribe(result => {});