question

bodin avatar image
bodin asked

CloudScript not run in consequence?

Hi,

while testing Cloudscript, I found some strange thing.

from the code below, it seems line #9 was run before line #8

handlers.TestSequence = function (args, context) {
	var testvar = {"TEST":[
		{"AA":1},
		{"AA":2},
		{"AA":3}
	]};
	
	log.debug("before push", testvar);	
	testvar.TEST.push({"AA":4});	
	log.debug("after push", testvar);
}

when I run this, I got this log

"Logs": [
            {
                "Level": "Debug",
                "Message": "before push",
                "Data": {
                    "TEST": [
                        {
                            "AA": 1
                        },
                        {
                            "AA": 2
                        },
                        {
                            "AA": 3
                        },
                        {
                            "AA": 4
                        }
                    ]
                }
            },
            {
                "Level": "Debug",
                "Message": "after push",
                "Data": {
                    "TEST": [
                        {
                            "AA": 1
                        },
                        {
                            "AA": 2
                        },
                        {
                            "AA": 3
                        },
                        {
                            "AA": 4
                        }
                    ]
                }
            }
        ],

I wonder why {"AA": 4} was already added the variable before the code reach line #9?

is it because log command will execute after all line of code?

thanks in advance & best regards,

Bodin

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
v-humcin avatar image
v-humcin answered

The log command executes in the expected order, however when you pass a reference to an object as the argument, it does not resolve the value of the reference until it builds the response at the end of code execution.

You would either want to build a new object with the desired values and use that as the argument or pass in the values directly as the argument.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.