When working with promises I've found it useful to consume functions regardless of them returning a plain value or a promise. Thus I can always chain these arbitrary functions in "promise-style" f1().then(f2). ... i.e. with Ramda R.composeP(f1, f2).
I think it would be useful to have this for Tasks as well:
var R = require('ramda'); // just for my convenience writing this implementation - sorry!
var toPromise = Promise.resolve.bind(Promise); // helper
// takes a function func which returns a value, Promise or Task and returns a function which
// will return a Task.
var taskify = function(func){
return R.compose(R.unless(R.is(Task), R.compose(Task.fromPromise, toPromise)) , func);
};
// examples:
taskify( (x, y) => x + y )(8, 9).runAndLog(); // -> Success: 17
taskify( (x, y) => Promise.resolve(x + y) )(8, 9).runAndLog(); // -> Success: 17
taskify( (x, y) => Task.of(x + y) )(8, 9).runAndLog(); // -> Success: 17
What do you think?
When working with promises I've found it useful to consume functions regardless of them returning a plain value or a promise. Thus I can always chain these arbitrary functions in "promise-style"
f1().then(f2). ...i.e. with RamdaR.composeP(f1, f2).I think it would be useful to have this for Tasks as well:
What do you think?