博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Compose] 10. Capture Side Effects in a Task
阅读量:4965 次
发布时间:2019-06-12

本文共 1190 字,大约阅读时间需要 3 分钟。

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

 

We using a 'data.task' library. It is a bit similar to $q library in Angular. Accepts one function, function takes two params, 'reject function' & 'resolve function'. 

import Task from 'data.task';const launchMissiles = () =>  new Task((rej, res) => {    console.log('launch missiles!')    res('missile')  })

 

Because this is laziness, therefore, we can compose logic based on that:

const app = launchMissiles().map(x => x + '!')
app.map(x => x + '!').fork(e => console.error('err', e),      x => console.log('success', x))

 

If inside 'lauchMissiles' call 'reject' function, all the map function chaining on app won't be called anymore.

'fork' is the actually function which trigger it works.

-----

import Task from 'data.task';const launchMissiles = () =>  new Task((rej, res) => {    console.log('launch missiles!')    res('missile')  })const app = launchMissiles().map(x => x + '!')app.map(x => x + '!').fork(e => console.error('err', e),      x => console.log('success', x))//launch missiles!success

 

转载于:https://www.cnblogs.com/Answer1215/p/6195461.html

你可能感兴趣的文章