Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taskify a function #33

Open
semmel opened this issue Jan 29, 2018 · 0 comments
Open

Taskify a function #33

semmel opened this issue Jan 29, 2018 · 0 comments

Comments

@semmel
Copy link

semmel commented Jan 29, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant