[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 ...
随机推荐
- 洛谷 P2978 [USACO10JAN]下午茶时间Tea Time
P2978 [USACO10JAN]下午茶时间Tea Time 题目描述 N (1 <= N <= 1000) cows, conveniently numbered 1..N all a ...
- 一些常用JS函数和技巧总结
1.JS原生函数parseInt(),返回字符串的第一个数字,默认是十进制. 2.!!data.success //强制转换成布尔类型
- valueof(), intvalue(0 parseint() 这三个方法怎么用
valueOf(int i) 返回一个表示指定的 int 值的 Integer 实例.valueOf(String s) 返回保存指定的 String 的值的 Integer 对象.valueOf(S ...
- iOS 之应用性能调优的25个建议和技巧
注意:每在优化代码之前,你都要注意一个问题,不要养成"预优化"代码的错误习惯. 时常使用Instruments去profile你的代码来发现须要提升的方面.Matt Gallowa ...
- (转)使用qemu-img管理虚拟机磁盘镜像(创建虚拟机,虚拟机快照)
转自:http://blog.csdn.net/bravezhe/article/details/8461386 一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系 ...
- [Angular] FadeIn and FadeOut animation in Angular
To define an Angular Animation, we using DSL type of language. Means we are going to define few anim ...
- Altium Designer的pcb上添加文字说明
- 9.1 Binder系统_C程序示例_框架分析和编写程序
IPC : Inter-Process Communication, 进程间通信 A进程把数据原原本本的发给B,这就是IPC RPC : Remote Procedure Call, 远程过程调用 A ...
- FPGA实现UHS的一些资料
对使用FPGA和SD卡进行UHS模式通信的评估: 论文:基于FPGA的SD UHS-II卡控制器设计与实现 设计IP:SD UHS-II Host Controller 供应商: System Lev ...
- The behavior of App killed or restored by Android System or by users
What's the behavior of App killed or restored by Android System or by users? First, user kills the a ...