217 points · by sacado2 · 12y ago · 74 comments
As of now, you could use Promises but it is still kind of messy without generators. Here is a simple example:
#Without generators
Credentials.get({ token })
.then (credentials) =>
User.get({ username: credentials.username })
#With generators
credentials = yield Credentials.get({ token })
User.get({ username: credentials.username })
We use FaceBook's regenerator project to compile ES6 features down to Node's currently supported Javascript features. If you don't want to use the --harmony flag, you should really try it out. https://github.com/facebook/regenerator credentials := NewCredentials(token)
User.findByName(credentials.userName)
The above is in Go, and it is just as fast if not more. Whilst all requests are on their own goroutines, the runtime will schedule them using the same efficient async io that does not need to be made visible as in JavaScript. var withThis = (object, f) => function bound() {
return f.apply(object, arguments);
}
function getCredentialsByToken(token) {
return newCredentials(token)
.then(withThis(User, User.findByName));
}This isn't a new thihg anyway, there are currently a lot of implementations, I even wrote one myself[1].
By the way, most (sane) libraries that use yield/await perfom a lot better, than vanilla promises. Because when you do .then() on already resolved promise you have to wait for the next tick, or setImmediate or whatewer. Yield-based implementations can continue right away in this case.
Actually I learned to embrace next-tickness of promises in Q because I don't have to think twice about different possible execution order depending on promise's current state.
I would honestly prefer immediate continuation to be opt-in. This worked well in .NET (its Task's ContinueWith has a flag called TaskContinuationOptions.ExecuteSynchronously that tells the scheduler “you can run me right away if you like”).
The requirement is to always calls handlers with an empty stack and in a certain order. In practice only naivest implementations would call platform scheduler for every `.then()`. Implementations can in all cases call the handlers synchronously when it would matter.
http://promises-aplus.github.io/promises-spec/#notes (emphasis mine)
The hardest stuff in JS is that :
function F(){throw "error"}
try{setTimeout(F,1000)}catch(error){ console.log("error")}
we cant catch async errors outside the async callback , will generators fix this ? ( by the way , that's why you should never throw any error yourself in js ).You can catch async errors with generators and promises; see e.g. the linked post, or http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaS...
You can inject an exception into a generator via the throw method[0], and it'll be raised at the yield, as if `yield` had thrown[1].
JS exception handling facilities are still crap, though.
[0] http://wiki.ecmascript.org/doku.php?id=harmony:generators#me...
[1] http://wiki.ecmascript.org/doku.php?id=harmony:generators#in...
I think you can catch async errors if you call the throw method on the generator.
http://wiki.ecmascript.org/doku.php?id=harmony:generators#me...
var just = property => object => {
var o = {};
o[property] = object[property];
return o;
}
Credentials.get({token})
.then(just('username'))
.then(withThis(User, User.get));
withThis available in a comment below. #Without generators
Credentials.get({ token })
.then(credentials => User.get({ username: credentials.username }))
#With generators
var credentials =
yield Credentials.get({ token })
User.get({ username: credentials.username })1. You write code almost like synchronous one. You can use any control-flow statements you are familiar with. Just dont forget to turn a promise into real value with `yield` keyword.
2. How much times have you wrote such statements?
X.prototype.foo = function() {
var self = this;
// or
var that = this;
this.bar().then(function(x) {
that.baz(x);
});
};
or
}.bind(this));With yield you can just write your logic, not a boilerplate:
X.prototype.foo = async(function*() {
var x = yield this.bar();
this.baz();
}); var a = yield A();
var b = yield B();
This is massively less performant than: var a = A();
var b = B();
a = yield a;
b = yield b;
In promises you naturally do `Promise.all([A(), B()]).spread((a, b) => //Use a and b);`2. Why did you stop using arrow functions?
X.prototype.foo = function() {
this.bar().then(x => this.baz(x));
};
3. You are overvaluing control-flow constructs. Try-catch is always a catch-all error silencer where as promises can extend `.catch()` and `.finally()` to overcome flaws. Hand written loops are not that common, one mostly uses .filter, .map and .reduce in these kind of work flows. try {
var user = yield Users.get(1234);
return user.name;
} catch(ex) {
// do whatever
}
rather than.. return Users.get(1234).then(
function (u) { return u.name; },
function (ex) { /* do whatever */ }
); Users.get(1234)
.then(u => u.name)
.catch(ex => 1/* do whatever */);Callbacks:
Backend.CallA(param, function() {
aDone = true;
if (aDone && bDone) { doC(); }
});
Backend.CallB(param, function() {
bDone = true;
if (aDone && bDone) { doC(); }
});
Promises: var promiseA = Backend.CallA(param);
var promiseB = Backend.CallB(param);
$q.all([promiseA, promiseB]).then(doC);http://blog.angularjs.org/2013/11/angularjs-120-timely-deliv...
The biggest issue I had with promises is interfacing with non-promise code. There are methods to 'lift' node.js code that uses callbacks into promises, but the resulting code is much noiser.
I'm a bit disappointed that the official JS version didn't cut down on the visual noise of the piping so the actual work being done stands out. Maybe there needs to be a different type of syntax highlighter that mutes the promises piping and highlights the actual calls.
Node tends to keep up with V8 versions pretty well, so I anticipate by the time Node 0.12 is released the --harmony flag will enable the Promise global. (Hopefully by then the V8 team will have gotten their act together on standards compliance.) Node core though is pretty committed to the error-first callback pattern; that will be a longer shift, I think.
[1]: https://twitter.com/promisesaplus/status/407721467778846720 [2]: http://www.html5rocks.com/en/tutorials/es6/promises/#toc-api
[1]: https://twitter.com/ChromiumDev/status/413035115665575936
The real problem is the core NodeJS libraries aren't promise compliant. I don't know if there's a module that cleanly wraps all of these in their promise analogs but that'd be the best thing here.
Calling Q.nsend() on everything is messy, but is a lot better than dealing with ridiculously nested callbacks.
var fs = Promise.promisifyAll(require("fs"));
And all of the regular methods in fs are now promisable. Q also seems to be one of the slowest[2] promise libraries there is. I'm not sure why so many people use it. That guy has a rather large benchmark table from a couple months ago as well and Q seems to be shockingly slow compared to the dozen or so other promise libraries out there.1. https://github.com/petkaantonov/bluebird
2. http://spion.github.io/posts/why-i-am-switching-to-promises....
This isn't a problem at all in practice.
To say that node core is not "promise compliant" gives people the idea that there is something that prevents them from using promises in node. In reality, most promise libraries have a "denodeify" function that will convert any function that expects a node-style callback into one that returns a promise instead.
var fs = require('fs');
var rsvp = require('rsvp');
// Create a version of fs.readFile that returns
// a promise instead of expecting a callback.
var readFile = rsvp.denodeify(fs.readFile);
readFile('/etc/passwd', 'utf8').then(function (contents) {
// do something with the file contents
}, function (error) {
// handle the error
});
It is similarly easy to convert functions that expect other types of callbacks (read: not node-style callbacks where the first argument is always an error) to functions that return promises using deferreds.Trouble is, NodeJS set a convention and now everyone adheres to it. I'm not complaining there's standards, in fact that's a good thing. Part of me is just disappointed that the promise standardization happened too late in the game for NodeJS to build around it.
There's a few of these in npm, but I think the best one is probably the pr package [1], which lets you do e.g. var fs = require("pr/fs") and then use Bluebird-promise returning versions of the fs methods.
Also, I'm not fully convinced that node is worth the code complexity that all of these solutions bring with them when go or scala can give you better performance with much cleaner code.
It works very well if you bear in mind what's happening to the call stack. In my case, every call to pause! or wait! is scheduling a continuation to be called later by the game loop. If I'm not mistaken, the overhead of each continuation is just that of a closure.
It's very comfortable, coming from a background in Stackless Python, Lua, and Unity.
I wish more people would take a look at ToffeeScript.
Is that what you were looking for?
The article linked looks to be at https://github.com/html5rocks/www.html5rocks.com/blob/master... .
http://www.whatwg.org/specs/web-apps/current-work/multipage/...
The biggest issue for me is Q's habit of hiding errors despite me defining a Q.onerror handler. Hopefully a native implementation would fail a little louder.
Q.longStackSupport = true
? This is invaluable for debugging (although it does slow things down a lot, so don't use it in production)