I have a problem, am running programm which will run every object throug foreach and write the data inside json file, but it always stop at some point, everytime its different. It doesnt give me any error, it just stop like normal would. And i ended up with 300 objects instead of 1400 i wanted. Any help?
function t (){
var jsonData = JSON.parse(fs.readFileSync("./list.json"));
jsonData.forEach((element) => {
request('https://api.elrond.com/accounts/'+element.address+'/tokens/count', function (error, response, html) {
if (!error && response.statusCode == 200) {
try{
var arr = JSON.parse((cheerio.load(html))('html').text())
}
catch(err){console.log(err)}
data = {
address: element.address,
ESDT_count: arr
}
console.log(data)
try{
var myObject = JSON.parse(fs.readFileSync("./list1.json"));
myObject.push(data);
var newData = JSON.stringify(myObject);
fs.writeFileSync("./list1.json",newData);
}
catch(err){console.log(err)}
}})
})
}
t();
i solved it with delay
async function a ()
{ data.forEach(async (person,i) => {
setTimeout(() => {
request('https://api.elrond.com/accounts/'+person.address+'/tokens/count', async function (error, response, html) {
if (!error && response.statusCode == 200) {
try{
var arr = await JSON.parse((cheerio.load(html))('html').text())
}
catch(err){console.log(err)}
console.log(arr)
var data ={ address: person.address ,
staked: arr }
var myObject = JSON.parse(fs.readFileSync("./list1.json"));
myObject.push(data);
var newData = JSON.stringify(myObject);
fs.writeFileSync("./list1.json",newData);
}})
}, i * 1000);
});
}
a()