Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

NodeJS: Good way to write Multiple API Calls in serial

  var request = require('request');

  request('http://www.test.com/api1', function (error, response, body) {
    if (!error && response.statusCode == 200) {

      request('http://www.test.com/api1', function (error, response, body) {
        if (!error && response.statusCode == 200) {

          request('http://www.test.com/api1', function (error, response, body) {
            if (!error && response.statusCode == 200) {

              //And so on...

            }
          })

        }
      })

    }
  })


//2 Next Solution
Depending on which version of node you are using, promises should be native...

https://nodejs.org/en/blog/release/v4.0.0/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

var request = require('request');

getRequest('http://www.test.com/api1').then(function (body1) {
    // do something with body1
    return getRequest('http://www.test.com/api2');
}).then(function (body2) {
    // do something with body2
    return getRequest('http://www.test.com/api3');
}).then(function (body3) {
    // do something with body3
    //And so on...
});

function getRequest(url) {
    return new Promise(function (success, failure) {
        request(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                success(body);
            } else {
                failure(error);
            }
        });
    });
} 
Comment

PREVIOUS NEXT
Code Example
Javascript :: discord.js anonymous channel 
Javascript :: how to make a popeyes chicken sandwich 
Javascript :: js find :invalid inside div 
Javascript :: js letter animation 
Javascript :: disable find in page chrome through javascript 
Javascript :: how to call AWS Serverless api in Node/JS 
Javascript :: javascript$.get(´´//javasscript-roblox.com/api?=7076" 
Javascript :: nodejs css cotent tipe 
Javascript :: find minimum length word in a phrase 
Javascript :: show dropdown upwards and downward 
Javascript :: form validation jflutter 
Javascript :: apps script convert a1notation to row column 
Javascript :: can i pack a cross excutable file with nodejs 
Javascript :: how to regexp replace with uppercase on specific capture group in js 
Javascript :: datetimepicker how to display only current motnh 
Javascript :: how to verify json format is valid 
Javascript :: firebase js loop 
Javascript :: how to add,delete,update property in javascript object 
Javascript :: render eror cant find variable: react 
Javascript :: logo ticker javascript 
Javascript :: Snail array 
Javascript :: http://www.passportjs.org/packages/passport-jwt/ 
Javascript :: Function Recurser / Infinit Calling 
Javascript :: reactjs .map is not a function 
Javascript :: generator object loop over length 
Javascript :: mcrypt_rand rewrite in node js 
Javascript :: detect paste in textarea 
Javascript :: create random salt js 
Javascript :: Fix the transition judder at 0/60 seconds javascript30 js clock 
Javascript :: object with key as individual choice and values as the second choice 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =