I am currently having a problem figuring out how to wait for a request to complete before returning any data. I do not believe that I can do this with Callback, and I could not find a good way to use EventEmitter for this. The reason I cannot use the callback is because my thread is currently working like this.
The request comes to the server> Generate XML> Contact the remote API for details to complete the XML generation> Finish XML Generation> Return the request to the client
The code that I currently have looks very similar to the code below.
Web server:
var xml = require('./XMLGenerator');
response.writeHead(200, {'Content-Type': 'text/xml'});
response.write(xml.generateXML());
response.end();
XML Generator:
function generateXML(){
var API = require('./API');
var response = API.getItems("5");
for(var i = 1; i <= response.length; i++)
{
}
}
Grabber API:
function getItems(sort_by, amount) {
var request = require("request")
var url = "https://url.com/api/get_items.json?amount=" + amount;
request({
url: url,
json: true
}, function (error, response, body) {
console.log('we got here!');
if (!error && response.statusCode === 200) {
var items = body.data.items;
console.log(items);
return items;
} else {
console.log("Error connecting to the API: " + url);
return;
}
})
}
"undefined", , . , XML , . ( psudeo, , , )
, , ?
EDIT: module/API, . , 2 , node .