javascript 在 node.js 中编写一个 while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32043347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
writing a while loop in node.js
提问by Ryan CrawCour
In c# I would do this -
在 C# 中,我会这样做 -
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.
一个不错的简单 7 行代码。node.js 中的等价物是什么?基本上需要一个检查条件的 while 循环,直到满足该条件,睡眠然后执行一些异步操作。
采纳答案by jozzy
There's a fundamental paradigm shift when you think in the Node.js way.
当您以 Node.js 的方式思考时,会发生根本性的范式转变。
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.
正如在节点中所写和所说的“除了你的代码之外,一切都并行运行”。JS 是单线程的,因此如果你让那个线程 sleep ,一切都会阻塞。
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.
这将设计您的异步 (performAsync) 操作以发出事件,然后在该事件发生时提供要执行的回调。
So it's even more compact and natural. Your code might look like
所以它更加紧凑和自然。你的代码可能看起来像
performAsync().on('result',function cb () {// do what pleases you});
回答by Inaimathi
In general, when you have a question of the form
一般来说,当你对表格有疑问时
"There's a thing I can do in language
A
; how do I do it in languageB
?"
“有一件事我可以用语言
A
来做;我如何用语言来做B
?”
check hyperpolyglot. It's a page that provides summaries of certain terms and concepts across various language classes.
检查hyperpolyglot。这是一个页面,提供跨各种语言类的某些术语和概念的摘要。
The scriptingpage shows you, among other things, how to use a while
loopin JS, Python, Ruby and PHP.
回答by Muhammet Arslan
Node.js, as you guess, its a javascript file. So you can use javascript codes. But there is a few differ about what should you use. In exmaple; with Node.js you can want to use sync while;
Node.js,如您所料,它是一个 javascript 文件。所以你可以使用javascript代码。但是对于您应该使用什么,有一些不同之处。例如;使用 Node.js,您可以同时使用同步;
var page = 2;
var last_page = 100;
(function loop() {
if (page <= last_page) {
request("/data?page=" + page, function (error, response, body) {
if (!error && response.statusCode == 200) {
store_data(body)
}
page++;
loop();
});
}
}());
On the example we call loop() function in loop(), so not on technically but on practically we are using loop.
在这个例子中,我们在 loop() 中调用 loop() 函数,所以不是在技术上而是在实际中我们使用循环。
Async example : async for loop in node.js
异步示例:node.js 中的异步 for 循环
回答by Slowmove
Take a look at https://github.com/caolan/asyncwhich have a lot of methods for such synchronous tasks.
看看https://github.com/caolan/async,它有很多用于此类同步任务的方法。