Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  68] [ 3]  / answers: 1 / hits: 22209  / 9 Years ago, mon, august 17, 2015, 12:00:00

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.


More From » node.js

 Answers
4

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});

[#65394] Thursday, August 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;