I’ve been impressed with an async await callback attempt in nodejs, but I don’t know if it’s the right thing to do. I have a function that takes hours, and I need to return a percentage of progress. I have tried doing a trial and error callback. which worked for me and I wanted to know what you think and if there is another more elegant way. I am working in cluster mode
import { setTimeout } from 'timers/promises';
let files = await generateFiles({
callback: e => console.log(e) //percentage of progress
});
const generateFiles = async ({ callback }) => {
var item = [1,2,3]
for (var x of item) {
callback(item.indexOf(x) * 100 / item.length)
await setTimeout(60000, 'sleep');
}
}