task-task
Postfix Task combinator without boilerplate
Last updated 5 years ago by bahmutov .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install task-task 
SYNC missed versions from official npm registry.

task-task

Postfix Task combinator without boilerplate

NPM

Build status semantic-release

Install

npm install --save task-task

Use

Suppose we have two functions, one that adds two numbers, and another one that doubles a given number.

const add = (a, b) => a + b
const double = x => x * 2

Instead of calling them directly, we can return Task objects.

const addTask = (a, b) => new Task((reject, resolve) => resolve(add(a, b)))
const doubleTask = x => new Task((reject, resolve) => resolve(double(x)))

Let us create concrete task, for example to add number 2 to number 3

const add2to3 = addTask(2, 3)

The beautify of Task objects is that we nothing is executed yet. We can append other operations to the task before executing it.

addTask(2, 3)           // nothing is executed yet
    .map(x => x - 1)    // nothing is executed yet
    .map(x => `2 + 3 - 1 = ${x}`) // nothing is executed yet
    .fork( // now runs all steps
        console.error,
        console.log
    )
// prints "2 + 3 - 1 = 4"

It is simple to transform task's result with another sync function before it runs, but how about combining results of two tasks?

Imagine we want to compare the sum of the add task with the doubled result of the double task and see if the sum is larger. The way to make a derived task from two tasks is to use chain method

const larger = (a, b) => a > b ? a : b
const add2to3 = addTask(2, 3)
const double5 = doubleTask(5)
const largerTask = add2to3.chain(sum => double5.map(doubled => larger(sum, doubled)))
largerTask.fork(..., ...)

The chain method grabs the result from the first task (its instance object), then grabs the result of the second task (using .map method) and finally runs the operator function on the two values. The returned result is automatically wrapped in the Task instance. We can write this as

function op(a, b) { ... }
const outputTask = task1.chain(value1 => task2.map(value2 => op(value1, value2)))

This is too long, and too much boilerplate. This is where task-task comes in handy. Instead of writing .chain .map ... functions, provide 3 arguments to get new Task. Same comparison between two tasks as above

const taskTask = require('task-task')
const larger = (a, b) => a > b ? a : b
const add2to3 = addTask(2, 3)
const double5 = doubleTask(5)
const largerTask = taskTask(add2to3, double5, larger)

That's it. Let me know if a complete reduction would be useful, as in

taskTask(task1, task2, op1, task3, op2)
// same as (task1 op1 task2) op2 (task3)

How is this different from a Promise?

Difference between a Promise and a Task

Related

Small print

Author: Gleb Bahmutov © 2016

License: MIT - do anything with the code, but don't blame me if it does not work.

Spread the word: tweet, star on github, etc.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2016 Gleb Bahmutov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (7)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |