zoukankan      html  css  js  c++  java
  • [Functional Programming] EitherToTask transform

    const Task = require("data.task");
    const Either = require("data.either");
    const { Right, Left } = Either;
    const { List } = require("immutable-ext");
    
    const eitherToTask = (e) => e.fold(Task.rejected, Task.of);
    const fake = (id) => ({
      id,
      name: "user1",
      best_friend_id: id + 1,
    });
    
    const Db = {
      find: (id) =>
        new Task((rej, res) =>
          setTimeout(() => res(id > 2 ? Right(fake(id)) : Left("not found")), 100)
        ),
    };
    
    const send = (code, json) =>
      console.log(`sending ${code}: ${JSON.stringify(json)}`);
    
    // FROM
    Db.find(3) // Task(Either(user))
      .chain((eu) =>
        eu.fold(
          (e) => Task.of(eu),
          (u) => Db.find(u.best_friend_id)
        )
      )
      .fork(
        (error) => send(500, { error }),
        (eu) =>
          eu.fold(
            (error) => send(404, { error }),
            (x) => send(200, x)
          )
      );
    
    // TO
    Db.find(3) // Task(Either(user))
      .chain(eitherToTask) // Task(user))
      .chain((u) => Db.find(u.best_friend_id)) // Task(Either(user))
      .chain(eitherToTask) //Task(user))
      .fork(
        (error) => send(500, { error }),
        (u) => send(200, u)
      );
  • 相关阅读:
    css水平垂直居中
    JavaScript提供了哪几种“异步模式”?
    sort()
    后端开发中,可以在Cache-Control设置的常用指令
    纯函数
    react和vue
    npm(classnames) 更灵活使用类名
    shell知多少?
    常见的HTTP状态码
    axios
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12921000.html
Copyright © 2011-2022 走看看