New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question: how to async with co #157
Comments
can't yield from within that callback, you'd have to change |
That was the one answer I was hoping I wouldn't get, haha. Thanks for the reply though, I'll probably have to write a custom xhr module / talk it over with @Raynos (: |
@yoshuawuyts You can always just wrap your xhr module in a thunk or a promise. Then you can yield that. |
@rickharrison what would that look like? Tried fumbling around with |
Or a promise example which might be easier to understand
|
@rickharrison Thanks! I was wondering though: where does edit: to clarify, we're doing an xhr request here so doing something synchronous won't cut it :( |
function *xhrmw(next) {
if (!this.url) return yield next;
var opts = {
url: this.url + qs.stringify(this.query),
body: this.body,
method: this.method,
headers: this.headers
};
var thunk = xhr.bind(null, opts);
var response = yield thunk;
this.response = response;
this.body = response.body;
// <needs a yield>
} |
Converting from callback to thunk is trivial just This means you can now yield on it. I do this everywhere when I do generators. This allows for a single async function definition that works with node, npm & plain callbacks but is also usable from within generators. |
@Raynos This is just wonderful, thank you so much! |
@Raynos does that have any benefits / drawbacks compared to this kind of thunk? https://github.com/repo-utils/org-labels/blob/master/bin/_org-labels#L93-L105 |
@Fishrock123 as a drawback http://stackoverflow.com/questions/8656106/why-is-function-prototype-bind-slow |
@Fishrock123 I use This puts zero burden on the author of the module and minimal burden on the user of the module in generators. |
As an alternative, I have written a module to handle this sort of pattern. function *xhrmw(next) {
if (!this.url) return yield next;
var marker = suspend();
var opts = {
url: this.url + qs.stringify(this.query),
body: this.body,
method: this.method,
headers: this.headers
};
xhr(opts, function(err, res, body) {
if (err) this.throw(err);
this.response = res;
marker.resume(err, body);
}.bind(this));
this.body = yield marker.wait();
} Shameless plug :) |
@yoshuawuyts an array of argument is just |
Woah, this somehow feels a bit like golang with the multiple response assignments. It's a shame that the |
@Raynos I've looked for documentation about this nature of |
Sometimes generators confuse the soul out of my body and I definitely don't want to be above asking help, so here goes:
How do you
yield
in co, within a callback without having the call stack unwrap itself. Also: how do you yield from a callback; passing in ageneratorFunction
directly stuff breaks.This is the function I'm trying to make work:
The text was updated successfully, but these errors were encountered: