[Functional Programming] Capture Side Effects in a Task / Async

We examine the data structure Task, see some constructors, familiar methods, and finally how it captures side effects through laziness.

 

We are going to check two libarays, one is 'data.task'. another is 'crocks/Async':

Install:

npm i -S data.task
npm i -S crocks

 

You can use 'of' construct method:

 Task.of(1)
    .fork(e => console.error(e), a => console.log(a)) // 1

 Async.of('U Wut M8')
  .fork(e => console.error(e),a => console.log(a)) // U Wut M8

 

You can do rejection:

 // Foucs to reject:
 Task.rejected('not work')   
 .fork(e => console.error(e), a => console.log(a)) // not work

 Async.Rejected('Async badguy')
  .fork(e => console.error(e),a => console.log(a)) // Async badguy

 

You can .map / .chain:

复制代码
 Task.of(1)
    .map(x => x + 1)
    .fork(e => console.error(e), a => console.log(a)) // 2


Task.rejected(1)
    .map(x => x + 1)
    .fork(e => console.error(e), a => console.log(a)) // 1 
    
Async.of(1)
    .map(x => x + 1)
    .fork(e => console.error(e),a => console.log(a)) //2
  
Async.Rejected(1)
    .map(x => x + 1)
    .fork(e => console.error(e),a => console.log(a)) // 1

Task.of(1)
    .map(x => x + 1)
    .chain(x => Task.of(x + 2))
    .fork(e => console.error(e), a => console.log(a)) // 4    

Async.of(1)
    .map(x => x + 1)
    .chain(x => Async.of(x + 2))
    .fork(e => console.error(e),a => console.log(a)) // 4  
复制代码

 

You can use 'contructor function':

复制代码
const lunchMissiles = () =>
    new Task((rej, res) => {
        console.log('lunchMissiles');
        res('missile!')
    });    
  
const lunchRocky = () =>
    Async((rej, res) => {
        console.log('lunchRocky');
        res('Rocky!')
    });       

lunchMissiles()
    .map(x => x + "!")
    .fork(e => console.error(e), a => console.log(a)) // lunchMissiles missile!!


lunchRocky()
    .map(x => x + "!")
    .fork(e => console.error(e), a => console.log(a)) // lunchMissiles missile!!    
复制代码

 

Finally, we can split the side effect without calling 'fork', and you compose with the rest of app:

复制代码
const taskApp =  lunchMissiles()
.map(x => x + "!");

const asyncApp = lunchRocky()
    .map(x => x + "!")


taskApp.map(x => "   From Task").fork(e => console.error(e), a => console.log(a))
asyncApp.map(x => "   From Async").fork(e => console.error(e), a => console.log(a))
复制代码

 

posted @   Zhentiw  阅读(247)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-02-26 [HTML5] Inlining images with SVG and data URIs
2016-02-26 [Unit Testing] Directive testing, require parent controller
2016-02-26 [Immutable.js] Lightning Fast Immutable.js Equality Checks with Hash Codes
2016-02-26 [SASS] Make a responsive arrow box
点击右上角即可分享
微信分享提示