0
Good afternoon, I am creating a code to read a text file and run line by line using the execa packages and list in the nodejs, but it is checking the task function first then executes the commands. Code is like this:
async function print(file_path, callback) {
fs.readFileSync(file_path.toString() , 'utf8', function(err, texto) {
if (err){
return callback (err);
}else {
var linhas = texto.split(/\rs?\n/);
linhas.forEach(function(linha){
const pRetry = require('p-retry');
const run = async () => {
const results = await execa(linha);
return results;
};
(async () => {
console.log(pRetry(run, {retries: 5}));
})();
});
}
})
}
const tasks = new Listr([
{
title: chalk.green('Criação de diretorios'),
task: () => print('teste.txt',function(texto,err){
if (err) {
throw new Error('Deve ser executado com root')
}
})
},
]); tasks.run(). catch((err: any) => { process Exit.(); });
out is like this:
If he’s showing off
Promise { <pending> }
is pq has something async that you’re not treating.– Allan Ramos
Instead of
console.log(pRetry(run, {retries: 5}));
if you dojs
const result = await run ();
console.log(pRetry(result, { retries: 5 }))


– Allan Ramos
I made the change that you said and no longer appears Promise {<pending>} , but in the execution of the tasks it already gives the ckeck in all task as done and runs in the background.. wanted him to execute a task and only after he finished calling the next task.
– Ronan Felipe de Mesquita
https://oieduardorabelo.medium.com/javascript-armadilhas-do-asyn-await-em-loops-1cdad44db7f0 . That is to have the async part inside a loop. It explains how to leave sequential respecting the array order.
– Allan Ramos