We refactor a standard node callback style workflow into a composed task-based workflow.

Original Code:

const app = () => {
fs.readFile('config.json', 'utf-8', (err, content) => {
if (err) throw err; const newContents = content.replace(//g, ''); fs.writeFile('config1.json', newContents, (err, _) => {
if (err) throw err;
console.log('success!');
})
});
} app();

Using Task:

const readFile = (filename) =>
new Task((rej, res) =>
fs.readFile(filename, 'utf-8', (err, content) => {
err ? rej(err) : res(content);
}));
const writeFile = (filename, content) =>
new Task((rej, res) =>
fs.writeFile(filename, content, (err, success) => {
err ? rej(err) : res(success);
})); const TaskApp = readFile('config.json')
.map(content => content.replace(//g, ''))
.chain(newContent => writeFile('config1.json', newContent)); TaskApp.fork(e => console.error(e),
x => console.log('success!'));

Using Async:

const Async = require('crocks/Async');
const fs = require('fs'); const readF = (filename) =>
Async((rej, res) =>
fs.readFile(filename, 'utf-8', (err, content) => {
err ? rej(err): res(content);
})); const writeF = (filename, content) =>
Async((rej, res) =>
fs.writeFile(filename, content, (err, success) => {
err ? rej(err) : res(success)
})); const AsyncApp = readF('config.json')
.map(content => content.replace(//g, ''))
.chain(newContent => writeF('config2.json', newContent));
AsyncApp.fork(
e => console.error(e),
x => console.log('success!!')
);

[Functional Programming] Use Task/Async for Asynchronous Actions的更多相关文章

  1. [Functional Programming] Reader with Async ADT

    ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...

  2. [Functional Programming] mapReduce over Async operations and fanout results in Pair(rejected, resolved) (fanout, flip, mapReduce)

    This post is similar to previous post. The difference is in this post, we are going to see how to ha ...

  3. [Functional Programming] mapReduce over Async operations with first success prediction (fromNode, alt, mapReduce, maybeToAsync)

    Let's say we are going to read some files, return the first file which pass the prediction method, t ...

  4. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  5. BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2

    In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...

  6. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  7. JavaScript Functional Programming

    JavaScript Functional Programming JavaScript 函数式编程 anonymous function https://en.wikipedia.org/wiki/ ...

  8. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  9. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

随机推荐

  1. java反射,代码优化

    java的反射机制属实强大,能解决好些问题 在接手别人写的代码的时候,有一个bean类的get方法特别low,我都看不下去 重复代码写五遍,我都觉得太不合理.之后将其中代码抽取出来修改了下. publ ...

  2. bazel使用汇总

    最近重构代码之后,打算在本地用bazel来作项目构建.主要是因为brpc已经支持了bazel,所以在此之前料想会简单许多. 安装比较简单,centos直接用yum就行.按照这个指示: https:// ...

  3. UESTC 1330 柱爷与远古法阵【高斯消元】

    题目链接[http://acm.uestc.edu.cn/#/problem/show/1330] 题意:有一个长度为L(L <= 300)的长廊,有一人站在最左边,他要到最右边去,他每次可以走 ...

  4. [BZOJ4785][ZJOI2017]树状数组(概率+二维线段树)

    4785: [Zjoi2017]树状数组 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 297  Solved: 195[Submit][Status ...

  5. Week One

    2018.11.21: 1.[BZOJ 4868][SHOI 2017] 从后往前枚举最后位置即可,如果$A<B$,用尽可能多的$A$替换$B$操作 Tip:很大的$C$可能爆$longlong ...

  6. UESTC 2015dp专题 H 邱老师选妹子 数位dp

    邱老师选妹子 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descr ...

  7. windows安装zookeeper单机版

    1.在apache的官方网站提供了好多镜像下载地址,然后找到对应的版本,目前最新的是3.4.6下载地址:http://mirrors.cnnic.cn/apache/zookeeper/zookeep ...

  8. Linux下使用SSH远程执行命令方法收集

    说明:可以做SSH免密登录之后执行,这样可以省去每次执行输入密码的提示. 对于简单的命令: 如果是简单执行几个命令,则: ssh user@remoteNode "cd /home ; ls ...

  9. JavaScript的最大函数參数长度和最大栈深度检測

    一般代码也许不会涉及最大參数长度和最大栈深度,但某些特殊场合,检測这两个參数还是有必要的.比如:用递归计算斐波那契数列的第n个值,不了解最大栈深度,难免显得肤浅.又比如:将一串charCode转成St ...

  10. jsp:include 动作指令 与 include 指令

    include动作指令可以在JSP页面中动态包含一个文件,这与include指令不同,前者可以动态包含一个文件,文件的内容可以是静态的文件也可以是动态的脚本,而且当包含的动态文件被修改的时候JSP引擎 ...