daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Aug 12th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var async = require('async');
  2. var request = require('request');
  3.  
  4.  
  5. app.get('endpoint',function(req, res){
  6.     async.series([
  7.         function(callback){request.get('url',callback)},
  8.         function(callback){request.get('url2',callback)},
  9.         function(callback){request.get('url'3,callback)},  
  10.     ],
  11.     function(err,results){
  12.         //handle error
  13.  
  14.         //results is an array of values returned from each one
  15.         var processedData = {
  16.             a: results[0],
  17.             b: results[1],
  18.             c: results[2]
  19.         };
  20.         res.send(processedDAta)
  21.     })
  22.  
  23. })
  24.    
  25. app.get('/endpoint',function(req, res){
  26.     var dataSources = ['url1', 'url2',url3];
  27.     var requestData = [];
  28.  
  29.     processRequestData = function(){
  30.     //do you stuff here
  31.     res.send(processedData);
  32.     };
  33.     dataSources.forEach(function(dataSource){
  34.         request.get(dataSource,function(err,result){
  35.             //handle error
  36.  
  37.             requestData.push(result);
  38.             if(requestData.length == dataSources.length){
  39.                 processRequestData();
  40.             }
  41.         })
  42.     })
  43. });
  44.    
  45. app.get('/resource', function(req, res){
  46.     // object to send back in the response
  47.     var results = {};
  48.  
  49.     // helper function to make requests and handle the data to results object
  50.     var getData = function(url, propertyName, next){
  51.       http.get(url, function(err, data){
  52.         // let async.js know if there is an error
  53.         if(err) return next(err);
  54.         // save data to results object
  55.         results[propertyName] = data;
  56.         // async.js needs us to notify it when each operation is complete - so we call the callback without passing any data back to
  57.         next();
  58.       });
  59.     };
  60.  
  61.     // array of operations to execute in series or parallel
  62.     var operations = [];
  63.  
  64.     operations.push(function(next){
  65.         getData('http://url-one', 'users', next);
  66.     });
  67.  
  68.     operations.push(function(next){
  69.     getData('http://url-two', 'posts', next);
  70.     });
  71.  
  72.     // async.js has a few options on how to execute your operations - here we use series
  73.     async.series(operations, function(err){
  74.         if(err){
  75.             throw err;
  76.         }
  77.         // if we get to this point, all of the operations have exectued and called
  78.         // next() without any errors - send the response with a populated results object.
  79.         res.send(results);
  80.     });
  81.  
  82. });
RAW Paste Data
Top