[Functional Programming] Use Task/Async for Asynchronous Actions
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的更多相关文章
- [Functional Programming] Reader with Async ADT
ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...
- [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 ...
- [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 ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- JavaScript Functional Programming
JavaScript Functional Programming JavaScript 函数式编程 anonymous function https://en.wikipedia.org/wiki/ ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- 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 ...
随机推荐
- DP 题集 2
关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...
- hihoCoder #1695 公平分队II
题目大意 Alice 和 Bob 在玩一个游戏.Alice 将 $1$ 到 $2n$ 这 $2n$ 个整数分成两组,每组 $n$ 个.Bob 从中选一组,剩下一组归 Alice.Alice 可以与 B ...
- MySQL启动项提权
关于MySQL的启动项提权,听其名知其意.就是将一段 VBS脚本导入到 C:\Documents and Settings\All Users\「开始」菜单\程序\启动 下,如果管理员重启了服务器, ...
- [YC703]ゴミ拾い Easy
[YC703]ゴミ拾い Easy 题目大意: 二维平面内有\(n(n\le3\times10^5)\)个人和\(n\)个物品,第\(i\)个人在\((a_i,0)\)上,第\(i\)个物品在\((x_ ...
- jsonp和jsonpcallback的使用
1. jsonp.jsonpCallback jsonp跨域时可以自定义的两个参数 2. jsonp: 回掉函数名的参数名,默认callback,服务端通过它来获取到回掉函数名 3. jsonpCa ...
- CentOS 6.9/7通过yum安装指定版本的PostgreSQL
PostgreSQL(10+) 一.安装PostgreSQL // 安装EPEL源 # wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel- ...
- Visual Studio 2015创建Shared Project时出错
今天使用Visual Studio 2015创建共享项目的时候发现如下错误: 网上搜了一下,发现了同样有人问这个问题的问题:Why can't I create Shared Project in V ...
- 汇编语言学习笔记(十二)-浮点指令----ACM
http://blog.csdn.net/q_l_s/article/details/54909328
- CMoLineMgr
#ifndef __E3GLOGOBJECTDB_H__ #define __E3GLOGOBJECTDB_H__ #include "PubCommon\Singleton.h" ...
- jdk1.8 foreach
lambda 表达式效率非常低,测试代码可以看到大概3~5倍的差距 遍历Map的方式有很多,通常场景下我们需要的是遍历Map中的Key和Value,那么推荐使用的: public static voi ...