This is a very simple promises library I wrote 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.
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 downstream error handlers).
On my PC, I read a file into memory & wrote it to another file 200,000 times using both callbacks & promises, and the difference in performance is negligible.
I think ~500ms for 200,000 promises is a fair tradeoff.
To run the benchmark:
A naive file copy example:
fs = require 'fs'promqueen = require 'promqueen'promoteErrCallback = promqueenpromoteErrCallbackreadFile = promoteErrCallbackfsreadFilewriteFile = promoteErrCallbackfswriteFile# Note: promoteErrCallback is used for callbacks with err as the 1st parameter. Otherwise, you can use promoteCallback if err isn't used (e.g. fs.exists)readFile__filenamethendatatoStringthenwriteFile 'temp.txt'resultfailthrow err
Once a promise fails, it propagates that failure (and its arguments) to all downstream handlers (with fail handlers being executed). Any value returned by a downstream fail handler is ignored.
Should downstream fail handlers be permitted to bring a failed state back to a resolved state, pass different arguments or halt the propogation? I'm not sure what the behaviour should be yet.
And also, should a "finally" type clause be implemented?