Skip to content

Taskify a function #33

Description

@semmel

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions