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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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()([[, ], [, ]] ...

  7. Perl6多线程2: Promise new/keep/bread/status/result

    来源于个人理解的翻译. 创建一个 promise: my $p = Promise.new; 可以打印运行 的Promise 状态: my $p = Promise.new(); $p.then({s ...

  8. Promise的前世今生和妙用技巧

    浏览器事件模型和回调机制 JavaScript作为单线程运行于浏览器之中,这是每本JavaScript教科书中都会被提到的.同时出于对UI线程操作的安全性考虑,JavaScript和UI线程也处于同一 ...

  9. Q promise API简单翻译

    详细API:https://github.com/kriskowal/q/wiki/API-Reference Q提供了promise的一种实现方式,现在在node中用的已经比较多了.因为没有中文的a ...

随机推荐

  1. java 钩子方法

    Runtime.getRuntime().addShutdownHook(shutdownHook);    这个方法的含义说明:        这个方法的意思就是在jvm中增加一个关闭的钩子,当jv ...

  2. 题目1205:N阶楼梯上楼问题(2008年华中科技大学计算机保研机试真题:递推求解)

    题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2447 解决:927 题目描写叙述: N阶楼梯上楼问题:一次能够走两阶或一阶,问有多少种上楼方式. (要求 ...

  3. phoenixframe自己主动化平台在Linux环境下运行用例的说明

    phoenixframe自己主动化平台支持在Linux环境下使用phantomjs,Firefox.chrome运行測试用例.但有下面几个问题须要注意: 1.若无法启动phantomjs,Firefo ...

  4. Mongodb总结2-Java版本的HelloWorld-CRUD例子

    2013年,写的CRUD太简单了,今天在原来的基础上,稍微完善了下,用了更多语法,比如排序sort.in语句等. 参考了<Mongodb权威指南-第1版-高清>,等下上传到CSDN下载频道 ...

  5. 原生js大总结六

    051.如何打印当前浏览器的版本等信息   navigator.userAgent   返回包含浏览器版本等信息的字符串 ,常用于判断浏览器版本及使用设备(PC或者移动端   052 .在浏览器地址栏 ...

  6. 【Educational Codeforces Round 33 B】Beautiful Divisors

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把所有的那些数字打表出来. 逆序枚举就好 [代码] /* 1.Shoud it use long long ? 2.Have you ...

  7. 各大免费邮箱邮件群发账户SMTP服务器配置及SMTP发送量限制情况

    网络产品推广和新闻消息推送时,经常用到的工具就是用客户邮箱发送邮件了,如果是要发送的邮件量非常大的话,一般的建议是搭建自己的邮局服务器,或者是花钱购买专业的邮件群发服务,免费邮箱的SMTP适合少量的邮 ...

  8. 页面事件(Init,Load,PreRender)执行顺序

    简介 对由 Microsoft® Internet 信息服务 (IIS) 处理的 Microsoft® ASP.NET 页面的每个请求都会被移交到 ASP.NET HTTP 管道.HTTP 管道由一系 ...

  9. 常用的Windows命令

    常用的Windows命令 explorer-------打开资源管理器 logoff---------注销命令 shutdown-------关机命令 lusrmgr.msc----本机用户和组 se ...

  10. 【AtCoder Beginner Contest 074 C】Sugar Water

    [链接]h在这里写链接 [题意] 让你在杯子里加糖或加水. (4种操作类型) 糖或水之间有一定关系. 糖和水的总量也有限制. 问你糖水浓度的最大时,糖和糖水的量. [题解] 写个dfs就好. 每次有4 ...