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.Even more, with yield you can easily achieve parallel execution just moving `yield` keyword to the place where you need actual value. Good luck doing this with promises.
2. Unfortunately, traceur-compiler does not support fat arrow yet;
3. If you use try-catch just as a silencer, man I have bad news for you. And if you provide real error-handling in `.fail()` then what’s the problem writing the same code inside catch clause?
There is just no way to use yield without it either being superfluous or sacrificing concurrency. Of course it is great when you have a sequence I guess, but promise code with arrows is not messy in comparison, maybe slightly more verbose at best.
Consider something like this which is far from optimal:
let suspend = require('suspend'),
request = require('request');
let getParsed = suspend(function* (urls) {
urls.forEach((url) => request(url, suspend.fork()));
return (yield suspend.join()).map((r) => parseBody(r.body));
});
This is again very easy to write inadvertently.Maximizing concurrency:
let getParsed = Promise.coroutine(function* (urls) {
return yield urls.map((url) => {
request(url).spread((response, body) => parseBody(body));
});
});
However, now that we have maximum concurrency, it is entirely pointless to even have a generator: let getParsed = urls =>
urls.map((url) => {
request(url).spread((response, body) => parseBody(body));
});
});
I said try-catch silences all errors that are not event meant to be handled but are bugs in code that should never be thrown in the first place. For example, if you have a typo in your code you will not know about that instead you will handle it like an expected error like network error.Since promise `.catch()` is not limited by the language, you can do e.g. `.catch(NetworkError, e => ());` or whatever.
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?
or }.bind(this));With yield you can just write your logic, not a boilerplate: