Answer the question
In order to leave comments, you need to log in
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
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question