Functional tasks serialization mini script (0.3kb)
import ruit from 'ruit'Serialize a list of sync and async tasks from left to right
Parameters
tasksany list of tasks to process sequentially
Examples
const curry = f => a => b => f(a, b)
const add = (a, b) => a + b
const addOne = curry(add)(1)
const squareAsync = (num) => {
return new Promise(r => {
setTimeout(r, 500, num * 2)
})
}
// a -> a + a -> a * 2
// basically from left to right: 1 => 1 + 1 => 2 * 2
ruit(1, addOne, squareAsync).then(result => console.log(result)) // 4Returns Promise a promise containing the result of the whole chain
Helper that can be returned by ruit function to cancel the tasks chain
Examples
ruit(
100,
num => Math.random() * num
num => num > 50 ? ruit.cancel() : num
num => num - 2
).then(result => {
console.log(result) // here we will get only number lower than 50
})Returns Symbol internal private constant
The same as ruit() but with the arguments inverted from right to left
Parameters
tasksany list of tasks to process sequentially
Examples
const curry = f => a => b => f(a, b)
const add = (a, b) => a + b
const addOne = curry(add)(1)
const squareAsync = (num) => {
return new Promise(r => {
setTimeout(r, 500, num * 2)
})
}
// a -> a + a -> a * 2
// basically from right to left: 1 => 1 + 1 => 2 * 2
ruit.compose(squareAsync, addOne, 1).then(result => console.log(result)) // 4Returns Promise a promise containing the result of the whole chain
ruit comes from the ruere latin verb that means It falls, It expresses properly the essence of this script and sounds also similar to run it