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. c3p0的经常使用配置方式

    1:第一种方式很easy c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3308/databas ...

  2. 1.Dubbo教程

    转自:https://blog.csdn.net/hellozpc/article/details/78575773 2. 什么是dubbo 2.1. 简介 DUBBO是一个分布式服务框架,致力于提供 ...

  3. django 简单会议室预约(1)

    django 是python的一个web框架,为什么要用django,作者之前用过另一个框架flask,虽然flask比较简单很容易让人学,但是flask没有整体感,会让初学着茫然. 这里我们用dja ...

  4. 水题ing

    T1: https://www.luogu.org/problemnew/show/P1724幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆 ...

  5. C#中对XML的操作

    现在有一个xml文件,名称:BookStore.xml,数据如下: <?xml version="1.0" encoding="gb2312"?>& ...

  6. ECMall 25个 数据库表 说明文档

    ecm_acategory //文章分类表 字段 类型 Null 默认 注释 cate_id int(10) 否   自增ID号,分类ID号 cate_name varchar(100) 否   分类 ...

  7. django-rest-framework框架 第二篇 之Mixin扩展类

    Mixin扩展类     ['列表操作','过滤','搜索','排序'] <一>:<1>创建项目: 配置 urls 主路由    配置model文件(举个例子,就以book为模 ...

  8. thinkphp5最最最最简单的ajax实例

    thinkphp5最最最最简单的ajax实例 一.总结 一句话总结:页面端使用$.get()方法传递ajax请求,服务器端判断是不是ajax请求,是的话接受参数,进行逻辑处理之后向客户端返回值. 1. ...

  9. ZOJ 2679 Old Bill ||ZOJ 2952 Find All M^N Please 两题水题

    2679:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1679 2952:http://acm.zju.edu.cn/onli ...

  10. IT从业人员关注哪些问题

    技术人员关注的问题非常多,但常见的至少有以下6种.特此整理,抓住核心问题,解决它. 一个人的精力和时间往往非常有限,能把核心问题都解决到位就是成功. 1.职业规划 大家从读小学开始,就是在为职业规划过 ...