1

I am trying to send, two calls using different urls ( they are not concurrent calls, first one will execute at first and after that second one can execute ). I am using Node 8 and using the request-promise library to have this executed. ( Everything happens in google cloud function ). The problem is, when there was only one url to execute, it worked fine. But when I added the second one, now both of the post requests is not working.

Here is my code:

  const confirmSms = {
    method: 'POST',
    uri: '.....',
    body: {
        quantity
    },
    json: true
};

 const options = {
    method: 'POST',
    uri:`....`,
    body: {
        amount
    },
    json: true
};

And, I am using async / await also. Now, how I executed is, this is the code :

 For the first one :

       await rp(confirmSms)

 For Second One :

       await rp(options)

Am I doing this correctly? What's wrong, and the quantity & amount is been got from the request ( so the value is available for sure ). Can I use various names or should I be using only "options"? Kindly help

Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
Dazzile Pro
  • 253
  • 3
  • 13

1 Answers1

0

you can use request-promise https://www.npmjs.com/package/request-promise

var request = require('request-promise');
var baseUrl = 'http://base_url';

request.post(baseUrl + '/url_01')
.then(function(body) {
  console.log("data 1: " + body);
}).then(function() {
    return request.post(baseUrl + '/url_2'); // <-- inside a function
}).then(function(body) {
   console.log("data 2: " + body);
}).catch(function (err) {
   console.error(err);
});
channasmcs
  • 1,104
  • 12
  • 27