I am a little new to node and I am completely new to koa. I am trying to use generators to create asynchronous web requests in an API, but I cannot figure out how to assemble all the parts.
As a note, I use bluebird because I saw some examples, and that seemed like a good idea. If there is an easier way to do what I want without a bluebird, that's fine too.
In my module:
plugin.searchForItem = function * (name) { Promise = require('bluebird'); request = Promise.promisifyAll(require('request')); console.log("making request"); yield request.getAsync('http://apisitegoeshere.com/apicall').then(function * (result) { var response = result[0]; var body = result[1]; console.log(response.statusCode); yield response; }); };
and I call it like this:
search.searchForShow = function (name) { data = this.plugins[0].searchForItem(name); console.log("search returned: " + data); console.log("search returned2: " + JSON.stringify(data.next())); console.log("search returned3: " + JSON.stringify(data.next())); return data; };
When I look at my console, I see:
search returned: [object Generator] making request search returned2: {"value":{"isFulfilled":false,"isRejected":false},"done":false} search returned3: {"done":true}
I know that my code is everything, but I worked on it for several hours, and I'm still not closer to fixing it.
Thanks!
source share