Wait for the request to complete Node.js

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(){
// Code to generate XML
var API = require('./API');
var response = API.getItems("5");
for(var i = 1; i <= response.length; i++)
{
  // more code to generate further XML using the API response
}

// Finish generating and return the XML
}

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 .

+4
2

. API- :

function getItems(amount, callback) {
// some code... 
request({
  url: url,
  json: true
}, function (error, response, body) {
   // some code...
   if (!error && response.statusCode === 200) {
      // some code    
      callback(items); <-- pass items to the callback to "return" it
   }
})
}

xml, :

function generateXML(callback){
// Code to generate XML
var API = require('./API');
API.getItems("5",function(response){
  for(var i = 1; i <= response.length; i++)
  {
    // more code to generate further XML using the API response
  }

  // Finish generating and return the XML
  callback(xml_result); // <-- again, "return" the result via callback
});
}

:

var xml = require('./XMLGenerator');
response.writeHead(200, {'Content-Type': 'text/xml'});
xml.generateXML(function(xmlstring){
    response.write(xmlstring);
    response.end();
});
+6

, request , .

API.getItems("5", function(rs){
      var response = rs;
      for(var i = 1; i <= response.length; i++)
      {
      // more code to generate further XML using the API response
      }

      // Finish generating and return the XML
    }

});


...
function getItems(sort_by, amount, callback) {...

...
callback(items); //Instead of return items;

...

return async, request . promises callbacks. .

undefined , var response = API.getItems("5"); , response undefined. , . \

response.writeHead(200, {'Content-Type': 'text/xml'});
response.write(xml.generateXML());
response.end();

- API http.createServer.

+1

Source: https://habr.com/ru/post/1589362/


All Articles