[Ramda] Convert a Promise.all Result to an Object with Ramda's zip and zipObj
In this lesson, we'll use Promise.all to get an array that contains the resolved values from multiple promises. Then we'll see how we can use Ramda to convert that array of values into a single object using zip with fromPairs. Then we'll refactor to use zipObj.
const R = require('ramda');
const {fromPairs, zip, zipObj} = R;
const getName = () => Promise.resolve('wan');
const getHobbies = () => new Promise((res, rej) => {
"use strict";
setTimeout(() => res(['basketball', 'skiing']));
});
Promise.all([getName(), getHobbies()])
// .then(console.log); // [ 'wan', [ 'basketball', 'skiing' ] ]
// Make it as object style
Promise.all([getName(), getHobbies()])
.then(([name, hobbies]) => ({name, hobbies}))
// .then(console.log); // { name: 'wan', hobbies: [ 'basketball', 'skiing' ] }
// Using zip & fromPairs
Promise.all([getName(), getHobbies()])
.then(zip(['name', 'hobbies'])) // [ [ 'name', 'wan' ], [ 'hobbies', [ 'basketball', 'skiing' ] ] ]
.then(fromPairs) // { name: 'wan', hobbies: [ 'basketball', 'skiing' ] }
// .then(console.log);
// zipOjb == zip + fromPairs
Promise.all([getName(), getHobbies()])
.then(zipObj(['name', 'hobbies']))
.then(console.log) // { name: 'wan', hobbies: [ 'basketball', 'skiing' ] }
[Ramda] Convert a Promise.all Result to an Object with Ramda's zip and zipObj的更多相关文章
- Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不支持的特性
mybatis插入数据时报错: Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or ...
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...
- [Ramda] Convert Object Methods into Composable Functions with Ramda
In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods ...
- [Ramda] Refactor a Promise Chain to Function Composition using Ramda
Promise chains can be a powerful way to handle a series of transformations to the results of an asyn ...
- [Ramda] Create a Query String from an Object using Ramda's toPairs function
In this lesson, we'll use Ramda's toPairs function, along with map, join, concatand compose to creat ...
- [Ramda] Pluck & Props: Get the prop(s) from object array
Pluck: Get one prop from the object array: R.pluck(}, {a: }]); //=> [1, 2] R.pluck()([[, ], [, ]] ...
- Perl6多线程2: Promise new/keep/bread/status/result
来源于个人理解的翻译. 创建一个 promise: my $p = Promise.new; 可以打印运行 的Promise 状态: my $p = Promise.new(); $p.then({s ...
- Promise的前世今生和妙用技巧
浏览器事件模型和回调机制 JavaScript作为单线程运行于浏览器之中,这是每本JavaScript教科书中都会被提到的.同时出于对UI线程操作的安全性考虑,JavaScript和UI线程也处于同一 ...
- Q promise API简单翻译
详细API:https://github.com/kriskowal/q/wiki/API-Reference Q提供了promise的一种实现方式,现在在node中用的已经比较多了.因为没有中文的a ...
随机推荐
- Android 内存监测工具
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/幻影浪子 [博主导读]俗话说:工欲善其事必先利其器!我们先来了解下内存监测工具是怎么使用的?为内 ...
- android插件式开发资料整理
1.DL : Apk动态载入框架 2.android中的动态载入机制
- swift -结构体
// // main.swift // Struct-Demo-05 // import Foundation println("结构体測试!") //结构体和C语言的结构体不同 ...
- CentOS6重启后DNS被还原的解决办法
CentOS6重启后DNS被还原的解决办法 http://luyx30.blog.51cto.com/1029851/1070765/ centos6.5的64位系统,修改完/etc/sysconfi ...
- animation-list -帧动画
帧动画实现起来比较简单,今天接触到使用xml来创建帧动画,记录下来. 它说白了,其实就是动态的展示图片而已 1.在xml中定义帧动画,如下 <?xml version="1.0&quo ...
- rhel5安装 oracle11
readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行 setup 命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- Android 迭代器 Iteraor迭代器以及foreach的使用
Iterator是一个迭代器接口,专门用来迭代各种Collection集合,包括Set集合和List集合. Java要求各种集合都提供一个iteratot()方法,该方法返回一个Iterator用于遍 ...
- Android新控件RecyclerView剖析
传智·没羽箭(传智播客北京校区Java学院高级讲师) 个人简单介绍:APKBUS专家之中的一个,黑马技术沙龙会长,在移动领域有多年的实际开发和研究经验.精通HTML5.Oracle.J2EE .Jav ...
- Web安全之Cookie劫持
1. Cookie是什么? 2. 窃取的原理是什么? 3. 系统如何防Cookie劫持呢? 看完这三个回答, 你就明白哪位传奇大侠是如何成功的!!! Cookie: HTTP天然是无状态的协议, 为了 ...