This is an attempt at getting a promise library to run as fast as native callbacks while still offering Promises/A+ compatability. It was sparked by these tweets:
We would not give up perf in core. You can already do this in userland if you have diff priorities.
It would take some work to optimise and you couldn’t use something like Q, but I’d be surprised if it couldn’t be done.
node promises will have to be as fast as "Base" (current callbacks) in this benchmark
It's intended as a proof of concept, The .then
method isn't quite good enough to pass the Promises/A+ test suite and crucially it doesn't have any way of handling non-truthy errors. I also haven't spent any time testing it, but it is principally possible to create a then
method that is compliant except for the flasy errors issue on top of this implimentaiton.
Note that I've not actually used the .then
method, instead I've created a much more optimised .nodeify
method. Anyone who uses promises seriously as promises will of course get the slight performance drop of using a real .then
method (probably a perf drop of about 30% - 40%). The point I wanted to make though is that you can make the .then
method available while still providing a high performance alternative for those who don't have complex control flow problems to manage.
Indeed creation of promise only when callback is not provided, leaves the door to maintain current performance in existing scripts, that's neat, but on the other hand it feels hacky and dirty, so not acceptable for node.
To have a clean and concise API I'd say that promise should always be returned from async function, and callback support should be kept for legacy reasons, so compatibility is not broken.
Maybe this change will be taken into account if promises really win as async standard for JS. Adoption is growing, but still tools like async are more popular, so we're not there yet ;-)