[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 ...
随机推荐
- 洛谷P1221 最多因子数 [搜索,数学]
题目传送门 最多因子数 目描述 数学家们喜欢各种类型的有奇怪特性的数.例如,他们认为945是一个有趣的数,因为它是第一个所有约数之和大于本身的奇数. 为了帮助他们寻找有趣的数,你将写一个程序扫描一定范 ...
- google::proto::message.h
整了一阵子google proto message.h, 遇到很多问题,各种百度.google ,估计是用的人不是很多,整的焦头烂额,很多API都不知道该怎么用,只能一点一点的扣,为了方便在这里先简 ...
- Python并发编程-信号量
信号量保证同一资源同一时间只能有限定的进程去访问 一套资源,同一时间,只能被n个人访问 某一段代码,同一时间,只能被n个进程执行 from multiprocessing import Process ...
- Android之 解析XML文件(1)—— Pull解析
(以下文章基本照抄<第一行代码>) 解析XML文件有很多方法,这里主要讲Pull解析和SAX解析.这篇文章主要是讲Pull解析. 一.Pull解析参考代码 先上代码: private vo ...
- Mock(模拟后端接口数据)配合Vuex的使用
1.下载Mock cnpm install Mockjs -S 2.新建一个data.js存放新生成的mock文件 编辑mock 并导出 const Mock = require('mockjs' ...
- FastReport.Net使用:[2]添加MSSQL数据源一
如何使用MSSQL表作为数据源 1.点击FastReport设计器中Data->Add Data Source菜单项,打开数据源添加向导. 2.添加新的数据连接. 点击 New connecti ...
- Opencv学习笔记2:图像模糊作用和方法
一.意义和作用: 图像的模糊处理就是将图片处理的更加模糊,如下图,左侧是原图,右侧是经过处理之后的图片. 从主观意愿上说,我们希望看到清晰的图像,而不是模糊的图像.所以很多时候我们听说还有一种专门进行 ...
- 【最小表示法】BZOJ1398-寻找朋友
[题目大意] 判断两个字符串是否循环同构. [思路] 我一开始的做法是直接同时在两个字符串上求最小表示法,只有部分测试点能过,理由未知,以后再来思考. 现在做法:分别求出两个字符串的最小表示法,再比较 ...
- AIDL原理之 Framewok层实现
AIDLFramework层的架构,如下图: 换而言之,Android就是在传统的C/S架构中加入了一层,实现IPC.图中表明,AIDL类似COM的Proxy/Stub架构.不过是现在android自 ...
- 今天测试了一下 sqlalchemy 性能
self.db.query(Users).filter(Users.Id==1).first() < self.db.execute('SELECT * FROM `users` WHERE ...