I'm somewhat new to node, and I'm completely new to koa. I'm trying to use generators to do async web requests to an API, but I can't figure out how to put all the pieces together.
As a note, I'm using bluebird because I saw some examples do that, and it seemed like a good idea. If there's an easier way to do what I want without bluebird, that's totally fine as well.
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'm calling 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 in 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 my code is kind of all over the place, but I've worked on it for hours and I'm still no closer to fixing it.
Thanks!