Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

javascript - Node.JS Wait for callback of REST Service that makes HTTP request

I am using express module to make a Restful API within Node.JS. In my service I am making additional http requests to outside Endpoints(server side) and I need to return the data from those http requests to my web service request body.

I have confirmed that if I use console.log on all the actions that the Web Service is conducting I am getting the data that I need. However, when I try to Return those values to the service they come back Null. I know that this is because of async and the callback is not waiting for the http request to finish.

Is there a way to make this work?

question from:https://stackoverflow.com/questions/16866015/node-js-wait-for-callback-of-rest-service-that-makes-http-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A common practice is to use the async module.

npm install async

The async module has primitives to handle various forms of asynchronous events.

In your case, the async#parallel call will allow you to make requests to all external APIs at the same time and then combine the results for return to you requester.

Since you're making external http requests, you will probably find the request module helpful as well.

npm install request

Using request and async#parallel your route handler would look something like this...

var request = require('request');
var async = require('async');

exports.handler = function(req, res) {
  async.parallel([
    /*
     * First external endpoint
     */
    function(callback) {
      var url = "http://external1.com/api/some_endpoint";
      request(url, function(err, response, body) {
        // JSON body
        if(err) { console.log(err); callback(true); return; }
        obj = JSON.parse(body);
        callback(false, obj);
      });
    },
    /*
     * Second external endpoint
     */
    function(callback) {
      var url = "http://external2.com/api/some_endpoint";
      request(url, function(err, response, body) {
        // JSON body
        if(err) { console.log(err); callback(true); return; }
        obj = JSON.parse(body);
        callback(false, obj);
      });
    },
  ],
  /*
   * Collate results
   */
  function(err, results) {
    if(err) { console.log(err); res.send(500,"Server Error"); return; }
    res.send({api1:results[0], api2:results[1]});
  }
  );
};

You can also read about other callback sequencing methods here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...