How to wait for the result of a Restful API in Nodejs

Asked

Viewed 999 times

5

I work with an Amazon JSON API, in which I research products and handle information according to their results, but within Nodejs the API information is written on the console, but it is not written in the call response. How to respond only after data returned from Amazon?

Follow the example of the code:

var http = require("http");
//Biblioteca para conexão e pesquisa nos servidores da Amazon 
var aws = require("aws-lib");

http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "application/json"});
    var prodAdv = aws.createProdAdvClient(yourAccessKeyId, yourSecretAccessKey, yourAssociateTag);
    var options = {SearchIndex: "Books", Keywords: "Javascript"};
    var resposta = "";
    prodAdv.call("ItemSearch", options, function(err, result) {
    console.log(result);
        resposta = result;
    });
    res.end(resposta);
}).listen(1337, "127.0.0.1");

PS: The function parameters createProdAdvClient have been amended for security reasons, for all purposes are completed.

2 answers

3


AWS services are intrinsically asynchronous. Of the two one, either you adopt this paradigm or you should use a library or module of Node JS transforming this paradigm asynchronous for a synchronous.

If you want to use a synchronous paradigm then you can test with Step

A simple control-flow library for Node.JS that makes Parallel Execution, serial Execution, and error Handling painless.

Use the Serial Execution option

See an example below:

A Function Step of step module accepts any number of functions with its arguments and runs serial in order using the this and moving to the new function in the next step.

Step(
  function readSelf() {
    fs.readFile(__filename, this);
  },
  function capitalize(err, text) {
    if (err) throw err;
    return text.toUpperCase();
  },
  function showIt(err, newText) {
    if (err) throw err;
    console.log(newText);
  }
);

Note that the this is passed in function fs.readFile. When reading the file ends the function Step sends the result as argument to the next Function of the string. Then the value returned by capitalize is passed on to showIt that displays the result.

With this we can synchronously chain the execution of methods.

  • 1

    This problem of understanding the asynchronous paradigm is so common that if you search for Synchronize asynchronous calls in node in Google you will find several implementations of modules that address this problem.

1

prodAdv.call("ItemSearch", options, function(err, result) {
console.log(result);
    resposta = result;
});
res.end(resposta);

Did you mean: (I simplified the code and fixed the error of using the http)

prodAdv.call("ItemSearch", options, function(err, result) {
  if (err) {throw err;}
  console.log(result);
  res.write(result);
  res.end();
});
  • 2

    The cause of the problem is worth mentioning: the service call is asynchronous, and the request is being terminated before the answer arrives.

  • The indemnity is confused and is using asynchronous methods as if they were not. For me it seemed only lack of organization, which generated confusion, or a badly copied example, for example of one who uses buffers.

  • Not the problem is the asynchronicity itself. I need a way for the server to respond only after the answer from the Amazon servers. @Gustavorodrigues I’ve tried the example you mentioned before and problem persists.

  • I just tested on Node and it seems that it was an error in using the http module/function.

  • Gustavo I managed, using the solution presented, the concept was to respond in the Callback function itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.