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

Convert nodebacks to Task #96

Closed
robotlolita opened this Issue Mar 22, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@robotlolita
Member

robotlolita commented Mar 22, 2017

Folktale should support converting Node-back style functions to Tasks.

A Nodeback function is a function that accepts a callback in Node-style, for example:

fs.readFile :: (pathname: String, callback: (Error or null, data: Buffer or String or null) => Void) => Void

A conversion function would take that readFile function and generate a new function that instead of taking a callback returns a Task object:

nodebackToTask(fs.readFile) :: (pathname: String) => Task Error (Buffer or String)

Where this goes?

In the data/conversions module, as nodebackToTask.
In the data/task module, as fromNodeback.

Refer to existing data/conversions modules, and the from* functions in data/maybe and data/either to see more or less how this is done.

@rpearce

This comment has been minimized.

Show comment
Hide comment
@rpearce

rpearce Apr 18, 2017

Contributor

@robotlolita I've just stumbled across this, but is this the sort of thing you're looking for? I wrote a little lib the other day for converting functions with callbacks to Tasks to solve a simple problem and was considering expanding upon it. It's also short enough to include here:

'use strict'

const Task = require('data.task')

function taskFromFn(fn, ctx) {
  return function() {
    var args = [].slice.call(arguments)

    return new Task(function(reject, resolve) {
      fn.apply(ctx,
        args.concat(function(err, data) {
          err ? reject(err) : resolve(data)
        })
      )
    })
  }
}

module.exports = taskFromFn

If so, I'd consider learning more about this library and taking a crack at a pull request!

Contributor

rpearce commented Apr 18, 2017

@robotlolita I've just stumbled across this, but is this the sort of thing you're looking for? I wrote a little lib the other day for converting functions with callbacks to Tasks to solve a simple problem and was considering expanding upon it. It's also short enough to include here:

'use strict'

const Task = require('data.task')

function taskFromFn(fn, ctx) {
  return function() {
    var args = [].slice.call(arguments)

    return new Task(function(reject, resolve) {
      fn.apply(ctx,
        args.concat(function(err, data) {
          err ? reject(err) : resolve(data)
        })
      )
    })
  }
}

module.exports = taskFromFn

If so, I'd consider learning more about this library and taking a crack at a pull request!

@robotlolita

This comment has been minimized.

Show comment
Hide comment
@robotlolita

robotlolita Apr 22, 2017

Member

@rpearce pretty much, yep :)

Member

robotlolita commented Apr 22, 2017

@rpearce pretty much, yep :)

@rpearce

This comment has been minimized.

Show comment
Hide comment
@rpearce

rpearce Apr 22, 2017

Contributor
Contributor

rpearce commented Apr 22, 2017

@rpearce rpearce referenced this issue Apr 25, 2017

Merged

Conversion from nodeback to task #116

5 of 5 tasks complete

rpearce added a commit to rpearce/folktale that referenced this issue Apr 26, 2017

(new task test) Implements Task.fromNodeback
 will allow you to pass node-style callback-accepting functions,
also known as "nodebacks", and convert them to Tasks.

Closes #96

rpearce added a commit to rpearce/folktale that referenced this issue Apr 26, 2017

(new task test) Implements Task.fromNodeback
 will allow you to pass node-style callback-accepting functions,
also known as "nodebacks", and convert them to Tasks.

Closes #96

@robotlolita robotlolita closed this in #116 Apr 26, 2017

robotlolita added a commit that referenced this issue Apr 26, 2017

(new task test) Implements Task.fromNodeback
 will allow you to pass node-style callback-accepting functions,
also known as "nodebacks", and convert them to Tasks.

Closes #96

@wafflebot wafflebot bot removed the ready label Apr 26, 2017

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