Closed alubbe closed 9 years ago
Hi Andreas,
Thanks for dropping by and for your thoughts!
The goal was to keep it extremely tiny and fast - so I wasn't thinking to extend the base much - but to rather perhaps have a second "extension" module (that could be built into one of course) that would have the extra features like you mention. (in fact, I slightly regret adding the timeout feature!)
You could easily get finally by placing this somewhere:
Zousan.prototype.finally = function(fn) { this.then(fn,fn); }
Promisification would likely be bigger - I'm not even sure how these work (scanning the function signature for "callback"?) - but regardless, it seems a good candidate for an extension module.
Then we could add rac() and other promise goodies!
Actually - better make that finally() return its then promise:
Zousan.prototype.finally = function(fn) { return this.then(fn,fn); }
Let me be cynical and ask if there really isn't room for a a single line of short code, that looks very gzip-able (is that a word?)? ;)
Regardless, single function promisification is definitely my more sought after feature. I found this in the bluebird benchmarks, so here is one such implementation:
function promisify(nodefn) {
return function() {
var self = this;
var l = arguments.length;
var args = new Array(l + 1);
for (var i = 0; i < l; ++i) {
args[i] = arguments[i];
}
return new Promise(function(resolve, reject) {
args[l] = function(err, val) {
if (err) reject(err);
else resolve(val);
};
nodefn.apply(self, args);
});
};
};
What is your vision on how the extension modules hook into zousan?
True, the finally
code is super small and well worth consideration. It doesn't seem to be part of the ES2015 Promise spec - but then again, neither is timeout...
Thanks for the promisification code bit (thanks Bluebird!) - so it assumes the last argument is always the callback - which is certainly the convention and perhaps true in all of nodejs. In fact, that was a downside of callbacks - whenever you wanted to expand a functions arguments, you had to either put the new argument in FRONT of the callback (breaking any existing uses of the function) or put them AFTER the callback (breaking the convention of the callback being last). It also made functions with variable arguments difficult.
Anyway - yah, that could be a useful utility - though it behaves more as a user of promises rather than part of the promise itself.
So, I was thinking that I would someday make a Zousan-ext package which would extend the Zousan prototype and provide utility functions like this promisify. It could just be a separate module within the Zousan package as well - perhaps it doesn't warrant its own package.. In either case, you could simply concat the extension after the Zousan.js file and it should work just fine, since Zousan is stored in the global space.
I'll send you a PR for finally. Feel free to reject it - no hard feelings.
On extending it, I understand you're thinking of hooking extra functions into the global Zousan object. I like that simple approach. If I get around to it, I might just put together a "zousan-promisify" package. It will be small, but it's worth its own package :+1:
Anyways, closing this issue because my questions are answered. Thanks!
Ok - sounds good. It may well be one of those things I don't think I need, but once its available I use it. It does make sense for "clean-up" tasks. Thanks for your work on this. Glad to have a contributor!
Hey Glenn, thanks for exploring the options for a small, high performance Promise library!
Just to let you know, I think it would be very helpful to have some kind of a promisification handler because both in the front-end and back-end you will frequently have to promisify the standard library.
My second most favourite thing would be a .finally handler.
Are those something you could envision for this repo?