T
T
Topsky2017-04-25 10:44:06
JavaScript
Topsky, 2017-04-25 10:44:06

Node.js what do I need, synchronous or asynchronous?

Hello, I need to make an unknown number of requests to the server until there is no json key in the response.

request({url:url}, function(err, response, body){
    let data = JSON.parse(body);
    while(data.nextPageToken !== undefined){
      var newUrl = url + '&pageToken=' + data.nextPageToken;
      request({url: newUrl}, function(err, response, body){
        newUrl = url + '&pageToken=' + JSON.parse(body).nextPageToken;
      })
    }
  })

The first request is sent, I get the nextPageToken, and I re-execute the request, already with the new pageToken received, and so on until the nextPageToken stops returning.
The number of iterations is unknown, earlier the code was like this for(var i = 0; i < data.pageInfo.totalResults / maxResults; i++), so I thought about while, but requests are all executed instantly, and the request itself is sent for 2- oh request.
So what do I need, asynchrony, or synchronism, I don’t understand what, how would I adequately implement this task?
--UPDATE
Simpler and clearer, you need to execute an unknown number of requests.
Each new request will consist of a part of the previous request, the finite number of iterations is determined by the absence of the desired key in the response.
It's just that while starts sending a bunch of hundreds of requests to the server that haven't yet been formed from a response.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2017-04-25
@Topsky

node v7.6+ or transpiling babel async2generators

function requestAsync(params) {
  return new Promise((resolve, reject) => {
    request(params, (err, response, body) => {
      if(err) { return reject(err); }
      resolve({response, body});
    });
  });
}

async function getURL(_url) {
  var data, url = _url;
  do {
    const {body} = await requestAsync({url});
    data = JSON.parse(body);
    url = _url + '&pageToken=' + data.nextPageToken;
  } while(data.nextPageToken !== undefined);
  return data;
}

T
Topsky, 2017-04-25
@Topsky

There is a solution like this

var page = 2,
    lastPage = 100;

async.whilst(function () {
  return page <= lastPage;
},
function (next) {
  request("http://some_json_server.com/data?page=" + page, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      store_data(body)
    }
    page++;
    next();
  });
},
function (err) {
  // All things are done!
});

But a third-party Async package is connected, and I would like to somehow do without it.
It seems that there were async, await in javascript, but it’s not really possible to use them
await request({}, function(){})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question