Tuesday 15 March 2016

[koajs] difficulty with asynchronous callbacks and yield

I've been working on a weather app in Node and being the Node noob that I am, I'm having some difficulty using callbacks in conjunction with yield. I've omitted a lot of other code because I don't think it's very relevant to my problem:router.get(function *(next) { let res; // current temperature (degrees K) in given city weather('London', config.API_KEY).then((temp) => { res = temp; }, (reason) => { console.log(reason); }); this.body = yield render('index.mustache', {temperature:res}); }); I know the reason that this won't work (async pains) but I've tried alternatives like yielding from the callback and setting this.body within the callback (this approach appeared promising but Koa sends a 404 in this case) but none of those seem to work. Any ideas on how I can make this work?edit: here's the complete code, just in case.

Submitted March 16, 2016 at 06:02AM by kbrgl

No comments:

Post a Comment