zoukankan      html  css  js  c++  java
  • [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' ] }
  • 相关阅读:
    语音
    elasticsearch-HQ 安装与使用
    身份证号归属地数据库
    mysql 检查一个字符串是不是身份证号
    导出微信聊天记录并生成词云
    云平台Linux主机安装流程
    7za的压缩与解压
    把linux文件夹压缩成tar.gz的命令
    Python中包(package)的调用方式
    golang学习笔记 ----读写文件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6544402.html
Copyright © 2011-2022 走看看