Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Created December 6, 2012 06:03
Show Gist options
  • Save michaelficarra/4222112 to your computer and use it in GitHub Desktop.
Save michaelficarra/4222112 to your computer and use it in GitHub Desktop.
comonadic futures on top of Q
Q = require './q'
class exports.Task
constructor: (@promise) ->
value: null
reason: null
result: ->
unless @promise.isResolved()
throw 'cannot ask for result of unresolved task'
if @reason?
throw @reason
@value
continueWith: (cb) ->
generateHandler = (fn) => (value) =>
t0 = new Task @promise
fn t0, value
next = cb t0
if next? then (Q.defer next).promise else null
success = generateHandler (t0, value) -> t0.value = value
failure = generateHandler (t0, reason) -> t0.reason = reason
new Task @promise.then success, failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment