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