folktale / data.task

Migrating to https://github.com/origamitower/folktale
MIT License
425 stars 45 forks source link

Feature request: optional arguments for fork. #29

Closed reggi closed 9 years ago

reggi commented 9 years ago

I'd really like it if .fork worked with one function just works and if there's an error it gets thrown (just like a promise would)

file.fork(function(value){ 
  console.log(value)
})

And I'd love support for a catch

file.fork(function(value){ 
  console.log(value)
}).catch(function(e){
  throw e
})

Note:

.fork is very similar to .then with a promise, the odd thing is that for promises

.then([Function fulfilledHandler] [, Function rejectedHandler ])

You can see the the rejectedHandler is the second argument.

.fork's arguments are reversed, which is really confusing.

robotlolita commented 9 years ago

Task uses the same argument order Either uses, so Left/Error is the first argument, Right/Success is the second.

I'm not a fan of optional arguments. I think they cause more problems than they solve (and they prevent VM optimisations too, because you get different shapes in your function call :/). But you can always create an utility function for that:

function run(task, cc) {
  task.fork(
    function(error){ throw error },
    function(value){ cc(value) }
  )
}

run(Task.of(1), console.log.bind(console))
run(Task.rejected(1), console.log.bind(console))

The next version of Task will separate things into Tasks and Futures, so it might have a simpler interface for running tasks in general (and it'll make the resource handling part simpler/more correct).