[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 ...
随机推荐
- Redis学习篇(三)之Key相关操作
KEYS 作用:返回所有的给定模式的key 语法:KEYS pattern 通配符: *: 任意个字符 ?: 任意一个字符 []: 匹配[]之间的字符 [a-z] [A-Z] \x: 匹配特殊字符 ? ...
- 【BZOJ 4027】 4027: [HEOI2015]兔子与樱花 (贪心)
4027: [HEOI2015]兔子与樱花 Description 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号 ...
- 加密url
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 可以采用 https 证书 双向 加密验证. 加密到 JNI 里面,还是可以通过抓包工具 ...
- padding Oracle attack(填充Oracle攻击)
最近学习到一种老式的漏洞,一种基于填充字节的漏洞.就想记录下来,早在2010年的blackhat大会上,就介绍了padding Oracle漏洞,并公布了ASP.NET存在该漏洞.2011年又被评选为 ...
- php -- 解决php连接sqlserver2005中文乱码问题(附详细解决方法)
@_@~~ --php5.2 --phpstudy --apache --sqlserver2005 @_@~~问题描述 问题一:php连接sqlsever2005,输入中文,然后查询sqlserve ...
- mybatis源码分析(2)-----SqlSession创建
1. 在创建好sqlSessionFactory之后,接着就要配置sqlSession的创建. <bean id="simpleTempalte" class="o ...
- SSM+Maven(教程一):学习SSM框架的前提条件。
准备工作 环境准备 1.配置jdk:http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html Intelij中配置JDK:File- ...
- OpenVPN Server端配置文件详细说明(转)
本文将介绍如何配置OpenVPN服务器端的配置文件.在Windows系统中,该配置文件一般叫做server.ovpn:在Linux/BSD系统中,该配置文件一般叫做server.conf.虽然配置文件 ...
- HDU 4496 D-City (并查集,水题)
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- Two-transistor circuit replaces IC
Linear Technology's recently introduced LTC4300 chip buffers I2C clock and data lines to and from a ...