• [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);
  • 相关阅读:
    react
    问题总结21-07-12至21-08-15
    排序
    问题总结21-06-28至21-07-11
    问题总结21-06-14至21-06-27
    问题总结21-05-29至21-06-13
    问题总结21-04-19至21-05-28
    问题总结21-03-29至21-04-18
    问题总结21-03-08至21-03-28
    问题总结21-03-01至21-03-07
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6208930.html
Copyright © 2020-2023  润新知