A Promise invokes a function which stores a value that will be passed to a callback. So when you wrap a Promise with an Observable, you'll always get that same value. This enables you to use the behavior as a caching mechanism when the Promises make requests for remote data.

  const p = new Promise((resolve, reject )=> {
console.log("Promise started"); // This line will be print out only once, when the promise was invoked
resolve(new Date());
}); // The output date should be the same, since promise was only invoke once
p.then(( date)=> {
console.log(date) // Thu Jul 19 2018 12:55:41 GMT+0300 (EEST)
}) setTimeout(( )=> {
p.then(( date)=> {
console.log(date) //Thu Jul 19 2018 12:55:41 GMT+0300 (EEST)
})
}, 2000);

Caching data in RxJS can be as simple as creating a caching function which can store the values in an object. This lessons walks through creating a caching function and explains how the function closes over an object then pairs a url with an Observable that returns the resolution of a Promise

    let cache = {};
// Cache function
const cachePerson = cache => url =>
cache[url] ?
cache[url]:
cache[url] = createLoader(url); const activeTab$ = this.$watchAsObservable('activeTab', {
immediate: true
}).pipe(pluck('newValue')); // this.$http.get() return a promise, then convert to Observable using from()
const createLoader = url => from(this.$http.get(url)).pipe(pluck('data')); const people$ = createLoader('https://starwars.egghead.training/people').pipe(
map(people => people.slice(0,7 ))
); const person$ = combineLatest(
activeTab$,
people$,
(people$, (tabId, people) => people[tabId].id))
.pipe(
map(id => `https://starwars.egghead.training/people/${id}`),
switchMap(cachePerson(cache)),
catchError(() => of({ name: 'Failed.. :(' })),
share()
);

[Vue-rx] Cache Remote Data Requests with RxJS and Vue.js的更多相关文章

  1. vue单文件组件data选项的函数体获取vue实例对象

    因配置的关系,导致 vue的data选项中存在事件.而事件无法获取 vue 的实例对象:项目是单文件形式的,以下代码只是例子 new Vue({ el:..., data:{ a: { onevent ...

  2. [GraphQL] Query Local and Remote Data in Apollo Link State

    In this lesson, you will learn how to query local and remote data in Apollo Link State in the same c ...

  3. Angular Multiple HTTP Requests with RxJS

    原文:https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs ----------------------------- ...

  4. Knockout Grid - Loading Remote Data

    http://wijmo.com/grid-with-knockout-viewmodel-loading-remote-data/ We were hearing quite a few peopl ...

  5. fastboot 刷system.img 提示 sending 'system' (*KB)... FAILED (remote: data too large)

    华为G6-C00卡刷提示OEMSBL错误,只能线刷 ,但是官方找不到线刷img镜像,无奈 网上下了个可以线刷的工具套件 流氓ROM . 使用HuaweiUpdateExtractor(工具百度)把官方 ...

  6. 【Vue】定义组件 data 必须是一个函数返回的对象

    Vue 实例的数据对象.Vue 将会递归将 data 的属性转换为 getter/setter,从而让 data 的属性能够响应数据变化.对象必须是纯粹的对象 (含有零个或多个的 key/value ...

  7. Vue实例的的data对象

    介绍 Vue的实例的数据对象data 我们已经用了很多了,数据绑定离不开data里面的数据.也是Vue的核心属性. 它是Vue绑定数据到HTML标签的数据源泉,另外Vue框架会自动监视data里面的数 ...

  8. 15.Vue组件中的data

    1.组件中展示数据和响应事件: // 1. 组件可以有自己的 data 数据 // 2. 组件的 data 和 实例的 data 有点不一样,实例中的 data 可以为一个对象 // 3. 但是组件中 ...

  9. Vue中用props给data赋初始值遇到的问题解决

    Vue中用props给data赋初始值遇到的问题解决 更新时间:2018年11月27日 10:09:14   作者:yuyongyu    我要评论   这篇文章主要介绍了Vue中用props给dat ...

随机推荐

  1. 使用jstack精确找到异常代码的

    https://blog.csdn.net/mr__fang/article/details/68496248

  2. (三)Mybatis总结之动态sql

    动态sql 为何需要动态sql?因为简单的sql语句已经不能满足复杂的业务需求 动态sql相当于sql语句拼接 1.if语句 if语句:判断,如果执行多条件查询,如果中间某个条件变量为空,就跳过当前判 ...

  3. Git命令add和commit的区别

    要想弄明白git add和git commit的区别,首先我们需要知道三个概念:工作区(Working Directory).版本库(Repository).暂存区(Stage or index). ...

  4. CSS布局整理

    目录 常用居中方法 水平居中 垂直居中 单列布局 二列&三列布局 float+margin position+margin 圣杯布局(float+负margin) 双飞翼布局(float+负m ...

  5. 树莓派连接arduino(USB串口通讯)

    2018-06-0115:12:19 https://blog.csdn.net/song527730241/article/details/50884890 重要步骤  查看端口:(ttyUSB0或 ...

  6. 安装ipython notebook及基本命令(快捷键)

    转载自:http://121.42.47.99/yuenshome/wordpress/?p=2622 目前基本上是Pycharm和ipython notebook结合起来做东西,ipython no ...

  7. Struts工作机制

    Struts工作机制? 为什么要使用Struts?工作机制:Struts的工作流程:在web应用启动时就会加载初始化ActionServlet,ActionServlet从struts-config. ...

  8. 远程图形界面:使用putty+xmin远程登录ubuntu-kde

    让我继续用反人类的编辑器Vim和emacs,我宁愿自断三指.因此,在Win端配置WinSCP+Putty+Xming远程操作ubuntu. 参考链接:putty+xming远程登录Ubuntu16.0 ...

  9. oracle查询性能优化

    原文http://www.cnblogs.com/cnjava/archive/2013/02/28/2937699.html 讲解的oracle数据库面对大数据如何优化查询.

  10. JsTree中节点添加CheckBox 以及 单选的实现

    stree中添加checkbox,需要在初始化时设置plugins属性: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $('#DpTree').data('jstree', fa ...