Skip to content
CoffeeScript JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Build Status

Direct port of python's Twisted Deferred module to coffeescript and thus javascript.



git clone git:// 
cd nodejs-twisted-deferreds
(sudo) npm link


  • coffeescript
  • nodeunit (for running the tests)


This is a contrived example that reads the contents of a file, trims the whitespace and prints the result to the console. If an error occurs then it is logged.

fs    = require('fs')
defer = require('twisted-deferred')
Deferred = defer.Deferred

# Create Deferred instance that will track the steps
d = new Deferred()

# Add a step that cleans the content
d.addCallback (content) ->
    return content.trim()

# Add a step that uses the cleaned content
d.addCallback (cleanedContent) ->
    console.log cleanedContent

# If there is an error at any step make sure it is logged.
d.addErrback (err) ->
    console.error err

# Grab the content from the file and start the procedure
fs.readFile "path", "r", (err, data) ->
    if err
        d.errback err
        d.callback data

It is also possible to wrap existing async code using the toDeferred method: d = defer.toDeferred(fs.readFile, "path") d.addCallback (content) -> console.log content d.addErrback (err) -> # log err


To run the tests ensure that nodeunit is installed. Then run

Something went wrong with that request. Please try again.