简体   繁体   English

解析云代码上的for循环的奇怪行为。

[英]Weird and surprising behavior of for loop on parse cloud code.

I am running a very basic hello world code on parse cloud code . 我正在解析云代码上运行一个非常基本的hello world代码。 I put a simple for loop in the cloud code and just to keep track wrote a console.log and printed the value of 'i'. 我在云代码中放入了一个简单的for循环,只是为了跟踪,所以编写了console.log并打印了“ i”的值。 This is how it looks like .. 这就是它的样子..

// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
   for(var i = 0;i<5;i++)
    {
      console.log("Count : " + i);
    }
response.success("Hello world!");
});

Ideally it should print the count sequentially .. But the output in my log is something like this .. 理想情况下,它应该按顺序打印计数..但是我的日志中的输出是这样的..

I2014-08-14T08:06:39.751Z] v9: Ran cloud function hello with:
  Input: {}
  Result: Hello world!
I2014-08-14T08:06:39.767Z] Count : 2
I2014-08-14T08:06:39.769Z] Count : 1
I2014-08-14T08:06:39.770Z] Count : 4
I2014-08-14T08:06:39.771Z] Count : 3
I2014-08-14T08:06:39.781Z] Count : 0

i need to call a function in a loop for my implementation. 我需要为我的实现循环调用一个函数。 but its not working properly. 但它无法正常工作。 Is there something wrong with for loop or is there some different way to implement the for loop in parse cloud code. for循环是否有问题,或者在解析云代码时是否有其他不同的方法来实现for循环。 I am clueless. 我无能为力。 Please help me with this issue. 请帮我解决这个问题。 Thanks in advance. 提前致谢。

There is nothing wrong. 没有任何错误。 You are just seeing the fact that Parse.com CloudCode and nearly all JavaScript you will encounter runs asynchronously. 您只是看到一个事实,Parse.com CloudCode和几乎所有将要遇到的JavaScript都是异步运行的。

console.log doesn't print immediately to the console like some code would running in a shell on your local system. console.log不会像在本地系统的外壳中运行的某些代码那样立即打印到控制台。 It's more like saying to the system "Please print this console.log message as soon as you can" it's up to the system when that gets executed. 这更像是对系统说“请尽快打印此console.log消息”,这取决于系统何时执行。 All that you know is that it will happen sometime. 您所知道的只是它会在某个时间发生。

Logging isn't that important (and Parse.com console sucks) so there isn't a way to control this behaviour but for operations where ordering is important, you can get a callback when the operation is complete in order to schedule your next operation. 日志记录并不那么重要(并且Parse.com控制台很糟糕),因此没有一种方法可以控制此行为,但是对于顺序很重要的操作,可以在操作完成后获取回调以安排下一个操作。 Eg 例如

console.log("Count : 0", { success: function() {
    console.log("Count : 1", { success: function() {
            console.log("Count : 2", { success: function() {
        }); 
    }); 
}); 

Note that this is just an example - you can't actually run that code because console.log doesn't support callbacks. 请注意,这只是一个示例-您实际上不能运行该代码,因为console.log不支持回调。 Check out Parse.Query which does. 查看Parse.Query的功能。

The code can get a little cumbersome and over indented so as you get further into it, you should start to learn how to use promises. 该代码可能会变得有些笨拙和缩进,因此当您深入研究它时,应该开始学习如何使用Promise。 See Parse.Promise. 请参阅Parse.Promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM