In c# I would do this -
double progress = 0;
while (progress < 100)
{
var result = await PerformAsync();
progress = result.Progress;
await Task.Delay();
}
A nice simple 7 lines of code. What's the equivalent in node.js ? Basically need a while loop that checks a condition, and until that condition is met, sleeps and then executes some async operation.
There's a fundamental paradigm shift when you think in the Node.js way.
As had been written and said about in node " Everything runs in parallel except your code" . JS is single threaded and hence if you make that thread sleep , everything blocks.
But if you model your problem in a natural way , it would be to design an async operation that would take its time to run and when its finished let it inform you of the same. Rather than you waiting for it to finish.
This you would design your async (performAsync) operation to emit events and then provide a callback to be performed when that event occurs.
So it's even more compact and natural. Your code might look like
performAsync().on('result',function cb () {// do what pleases you});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With