promqueen

Really simple promises library

promqueen

This is a very simple promises library I wrote in an afternoon, mainly after reading https://gist.github.com/3889970. I wanted to implement them out of curiosity, but it turned out to be a little harder than I thought.

Promises are a beautiful solution to nested callbacks, and unless you're writing high performance code (in which case I would ask why you're using Javascript), I believe the payoff in developer productivity is much greater than the performance overhead imposed by the library. See below for benchmarks.

I haven't set out to replicate CommonJS promises (Promises/A, B, etc.), but IMHO, this library has got the gist of it (i.e. chaining promises & capturing error handlers downstream).

Enjoy!

A naive file copy example:

fs = require 'fs'
promqueen = require 'promqueen'
 
promoteNodeCallback = promqueen.promoteNodeCallback
 
readFile = promoteNodeCallback(fs.readFile)
writeFile = promoteNodeCallback(fs.writeFile)
 
readFile(__filename).then (err, data) ->
    data.toString()
.then (result) ->
    writeFile 'temp.txt'result
.fail (err) ->
    throw err

On my PC, I read a file into memory & wrote it a file 200,000 times using both callbacks & promises, and the difference in performance is negligible.

  • callback elapsed: 10472ms
  • promqueen elapsed: 11175ms

To run the benchmark: coffee test/bench.coffee