0

I need to fetch some results from an endpoint and the way it is structured, the response tells me what endpoint to use next for the next set of results (current limit of 1k rows per request), until all results have been obtained. I then need to stitch the data back together.

I can relatively easily fetch one set of results using the following code, but struggle to understand how I can wait for the results in order to fetch the next set, unless I nest it, and I don't want to do that as I don't know how many sets of results I need to fetch upfront.

var headers = {
  'Content-type': 'application/json',
  'X-DBX-AUTH-TOKEN': 'yyy@zzz.com/6573846tf9334e'
};

var options_results = {
  method: 'GET',
  headers: headers,
};

var url = 'https://endpoint/execute/{uuid}/0';
options_results.url = url;

function fetchResults(error, response, body) {
  console.log(body);
  var next_uid = JSON.parse(body.uuid);
}

request(options_results, fetchResults);

I have read up on promises etc, but am still struggling how to apply that here. Any help would be really appreciated!

kd1978
  • 487
  • 5
  • 21
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Matt Morgan Jan 13 '18 at 13:54

1 Answers1

0

You can turn your request callback function into a promise. Code would look like this:

var requestPromise = options =>
  new Promise(
    (resolve,reject)=>
      request(
        options,
        (error,response,body)=>
          (error)
            ? reject(error)
            : resolve([body,response])
      )
  );

requestPromise(
  {
    url:'https://endpoint/execute/{uuid}/0',
    method: 'GET',
    headers: {
      'Content-type': 'application/json',
      'X-DBX-AUTH-TOKEN': 'yyy@zzz.com/6573846tf9334e'
    }
  }  
)
.then(
  ([body])=>{
    var next_uid  = JSON.parse(body.uuid);
    //make next request
    return requestPromise(
      {
        url:'https://endpoint/execute/{uuid}/0',
        method: 'GET',
        headers: {
          'Content-type': 'application/json',
          'X-DBX-AUTH-TOKEN': 'yyy@zzz.com/6573846tf9334e'
        }
      }          
    )
  }
)
.then(
  ([body,response])=>{
    console.log("made other request:",body,response);
  }
)
.catch(
  err=>console.error("something went wrong:",err)
);
HMR
  • 37,593
  • 24
  • 91
  • 160