R
R
rockwell3232020-06-24 12:26:24
JavaScript
rockwell323, 2020-06-24 12:26:24

How to send data one by one in a post request?

I have a lot of data in sub-arrays, after sending the first sub-array in a post request, I wait for a response, then after receiving a response, I try to send the second sub-array, etc. (waiting for a response - I send the next sub-array).
It turns out only with the first subarray, it goes around in a circle. How can I sequentially send data in a post request after receiving a response (that is, sent the 1st subarray - waited for a response, sent the 2nd subarray - waited for a response, etc.) ????
Here is an example of code, I tried to substitute in the form mis[1] mis[2] mis[3], etc. through cycles, but it doesn’t work, it doesn’t wait for a response, data is constantly sent, which causes errors Frequent requests. Tried through while after if (response.statusCode == 200) {i++} loop doesn't want to work. In general, something like this (

async function subscribe(){
  request.post({
          url: 'https://example.com',
    form: {list: mis[0]}  --> // первый подмассив
         }, async function(err, response, body){
               console.log(response.statusCode);
               if (response.statusCode == 200){
                    await subscribe();
               };
        });
subscribe();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-24
@rockwell323

async is completely redundant in your case

function subscribe (ind = 0) {
  request.post({
      url: 'https://example.com',
      form: {list: mis[ind]} 
    }, (err, response, body) => {
      console.log(response.statusCode)
      if (response.statusCode == 200) 
        subscribe (ind + 1)
   })
}
subscribe()

True, it is not very clear why you are waiting for a response every time if this response results are not used in subsequent requests. What's the point then?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question