zoukankan      html  css  js  c++  java
  • [Compose] 19. Leapfrogging types with Traversable

    We use the traversable instance on List to reimplement Promise.all() type functionality.

    For example we want to conver:

    [Task] => Task([])

    Conver array of Task, into Task of array value.

    To do that we can use traverse

    Noraml Javascript array doesn't have traverse method, so we need to bring immutable-ext.

    const fs = require('fs')
    const Task = require('data.task')
    const futurize = require('futurize').futurize(Task)
    const { List } = require('immutable-ext')
    
    const readFile = futurize(fs.readFile)
    
    const files = List(['box.js', 'config.json'])
    
    files.traverse(Task.of, fn => readFile(fn, 'utf-8'))
    .fork(console.error, console.log)

    Here 'futurize' give us a easy way to wrap a function into a Task. We can do it manually as well:

    /*
    const readFile = (filename, encode) => new Task((rej, res) => {
        return fs.readFile(filename, encode, (err, content) => {
            if (err) rej(err);
            res(content);
        });
    });*/
    const readFile = futurize(fs.readFile);
  • 相关阅读:
    WSL+vscode安装rust注意事项
    几种判断质数的算法
    select被遮挡问题
    idea工具使用
    docker部署nexus服务
    docker部署nacos
    springcloudAlibaba整合nacos
    Navicat_Premium_v15 激活
    CF295D Solution
    CF351B Solution
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6208930.html
Copyright © 2011-2022 走看看