freeze-task
v1.0.0
Published
Create tasks that compute only once.
Downloads
3
Readme
Freeze Task
Ensure a tasks computation is executed at most once, returning the same result each time it is forked.
When a task is forked twice its default behavior is two execute its computation twice. A frozen task computes once when fork is called the first time, caching the result for later forks.
var Task = require('data.task'); // Use any Task library here.
var freeze = require('freeze-task')(Task);
var counter = 0;
var task = new Task((reject, resolve) => resolve(counter++))
// Each time task is forked it's computation is executed.
task.fork(console.log, console.log) // => 0
task.fork(console.log, console.log) // => 1
// The computation of a freezed task is only executed a single time.
var frozenTask = freeze(task)
frozenTask.fork(console.log, console.log) // => 2
frozenTask.fork(console.log, console.log) // => 2
Note that the above example is meant merely to illustrate what the library does. The case where you're expecting different results when forking a task multiple times is most likely precisely the sort of situation where you do not want to freeze.