4 ms·
Well yes you can achieve better concurrent execution if you essentially duplicate the code. I also showed you how to do it with promises already (in 1 line) so
by esailija 13y ago
Well yes you can achieve better concurrent execution if you essentially duplicate the code. I also showed you how to do it with promises already (in 1 line) so not sure why you are wishing me good luck :)
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.