Friday 23 November 2018

request-promise, return value

I know this must be a common problem but I am yet to find the a working answer after many hours of digging so I am going to ask for help. Chaining the requests works as expected, however I am having trouble ending up with the result of the final web call being returned. I do understand that its the async nature of node, but I don't know how to get around it.const request = require('request-promise') const options = { method: 'GET', uri: 'https://ift.tt/1HYwxQm' } //should make multiple web requests and return the final value var x = request(options) // First call would be to get a JWT .then(function (response) { console.log('first:' + response); //need to modify some of the options for second call for data options.uri = 'https://ift.tt/2AflKjC'; }).then(function(){ return request.get(options); }).then(function(response) { //this is what I want returned console.log('second:' + response) }).catch(function(err) { console.error(err); }); console.log('Return value:' + x) The result:Return value:[object Promise] first:6 2 3 2 2 6 3 1 6 2 second:2 1 1 2 3 Everything works within the request, but since the final console.log runs before the requests have completed, it's just returning a promise and I need it to return the value of the final call.​Any help appreciated.Thanks!

Submitted November 23, 2018 at 11:42PM by Jacksonp2008

No comments:

Post a Comment