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. HDU 4085 Peach Blossom Spring

    斯坦纳树. 最后可以是森林,在计算出每个联通状态的最小费用后,还需要进行一次$dp$. #include<bits/stdc++.h> using namespace std; const ...

  2. 洛谷P4171 [JSOI2010] 满汉全席 [2-SAT,Tarjan]

    题目传送门 满汉全席 题目描述 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉 ...

  3. Python并发编程-进程池回调函数

    回调函数不能传参数 回调函数是在主进程中执行的 from multiprocessing import Pool import os def func1(n): print('in func1', o ...

  4. Java反射机制demo(一)—实例化Class对象,并获得其他类包名和类型

    Java反射机制demo(一)——实例化Class对象,通过对象获得包名和类型 使用Java中的 本地类作为实验对象,避免自定义的类带来的一些不便. public static void main(S ...

  5. 【BZOJ 3289】 3289: Mato的文件管理 (莫队)

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2819  Solved: 1185 Description Mato同 ...

  6. Ubuntu系统安装网易云音乐、搜狗输入法

    这两个软件都很良心,提供了Ubuntu版本,直接下载安装即可. 网易云音乐: 下载-打开-安装 http://music.163.com/#/download 搜狗拼音输入法 下载-打开-安装 htt ...

  7. Android UI框架基本概念

    Activity:基本的页面单元,Activity包含一个Window,window上可以绘制各种view View:最基本的UI组件,表示屏幕上的一个矩形区域: Window:表示顶层窗口,管理界面 ...

  8. Problem A: 深入浅出学算法002-n个1

    Description 由n个1组成的整数能被K(K<10000)整除,n至少为多少? Input 多组测试数据,第一行输入整数T,表示组数 然后是T行,每行输入1个整数代表K Output 对 ...

  9. bzoj 2820

    收获: 当一个东西的取值有限时,我们可以枚举它,然后统计它被计算了多少次. #include <cstdio> #include <iostream> using namesp ...

  10. [转]Jquery实现页面定时跳转

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...