Answer the question
In order to leave comments, you need to log in
How to synchronously send a request when reading a file line by line?
I've only recently been introduced to JS and asynchronous programming, so I still struggle when I encounter asynchronous code execution.
The goal is to test one service, for this I uploaded to a JSON file all the records from which the URLs for requests are formed. And you need to check each entry by sending a separate request for it. 10k records were successfully uploaded, everything is readable, but when I started reading the file line by line, I ran into a problem that requests go asynchronously to unformed URLs, and 500 errors are returned accordingly.
To send requests, I use request-promise.
For line-by-line reading byline
var user = api.webAuthorize(qaqaqa);
user.then(() => {
var file = './assets.json';
var stream = byline(fs.createReadStream(file, { encoding: 'utf8' }));
stream.on('data', (line) => {
var parsedLine = JSON.parse(line);
api.getPlayList(parsedLine.asset).then((body) => {
//обработка тела ответа
});
});
});
Answer the question
In order to leave comments, you need to log in
The problem in the end turned out to be not even in asynchrony, although it looked like it, but in speed. The server did not have time to process and answered a little incorrectly)
It was just necessary to add delays.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question