From a cloud function: how to call JavaScript function that is recursive?

Henry Situ

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?  

function testrecur(s) {
    console.log("begin testrecur=" + s);
    s++;
    if (s < 10) {
        testrecur(s);
    } else {
        return s;
    }
}
Parse.Cloud.define("testrecursion", function(request, response) {
    Parse.Promise.as().then(function() {
        return testrecur(0);
    }).then(function(Result) {
        response.success(Result);
    }, function(error) {
        response.error(error);
    });
});

Executing testrecursion returns no errors in console.

The info console log shows

I2015-10-10T08:55:17.308Z]begin testrecur=0
I2015-10-10T08:55:17.309Z]begin testrecur=1
I2015-10-10T08:55:17.315Z]begin testrecur=7
I2015-10-10T08:55:17.316Z]begin testrecur=8

Executing testrecursion again shows this in the info console log.

I2015-10-10T08:19:15.970Z]begin testrecur=0
I2015-10-10T08:19:15.971Z]begin testrecur=1
I2015-10-10T08:19:15.972Z]begin testrecur=2
I2015-10-10T08:19:15.973Z]begin testrecur=3
I2015-10-10T08:19:15.974Z]begin testrecur=4
I2015-10-10T08:19:15.975Z]begin testrecur=5
I2015-10-10T08:19:15.978Z]begin testrecur=8

Executing testrecursion the 3rd time shows this in the info console log.

I2015-10-10T08:22:14.729Z]begin testrecur=2
I2015-10-10T08:22:14.731Z]begin testrecur=4
I2015-10-10T08:22:14.732Z]begin testrecur=5
I2015-10-10T08:22:14.733Z]begin testrecur=6
I2015-10-10T08:22:14.734Z]begin testrecur=7

After testing this dozens of times, the recursive steps appear to be called sporadically. The output seems to be random. The expected output is

I2015-10-10T08:19:15.970Z]begin testrecur=0
I2015-10-10T08:19:15.971Z]begin testrecur=1
I2015-10-10T08:19:15.972Z]begin testrecur=2
I2015-10-10T08:19:15.973Z]begin testrecur=3
I2015-10-10T08:19:15.974Z]begin testrecur=4
I2015-10-10T08:19:15.975Z]begin testrecur=5
I2015-10-10T08:19:15.975Z]begin testrecur=6
I2015-10-10T08:19:15.975Z]begin testrecur=7
I2015-10-10T08:19:15.975Z]begin testrecur=8
I2015-10-10T08:19:15.975Z]begin testrecur=9

Does this look like the recursion is happening correctly, and it's just that the console log is not logging all the messages?

I'm trying to implement what Hector Ramos mentioned in https://www.parse.com/questions/error-too-many-recursive-calls-into-cloud-code You can use recursion, you just can't recursively call Cloud Functions since that Cloud Function request will execute on a different thread. Use regular JavaScript functions, initiated from a Cloud Function, instead. - Héctor Ramos about 2 years ago

Henry Situ

It turns out that testrecur() needs to return a promise so that the caller, in this case, testrecursion() will wait for testrecur() to complete before starting the next chain in the promise. The actual code is available at How to return a value from a regular javascript function that contains a promise chain?

By the way, the recursion code in this question is correct because the recursion is happening correctly. We just need to tie the promises in sequence so each recursive call can have the opportunity to execute completely before the calling function completes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

From a cloud function: how to call JavaScript function that is recursive?

From Dev

How to call a recursive function in javascript

From Dev

Javascript: How to write and call a recursive function?

From Dev

How to call php function from JavaScript function?

From Dev

how to call javascript function from SmartGWT function

From Dev

How do I return javascript data from a function using a recursive call

From Dev

JavaScript recursive function breaks when () included in call

From Dev

JavaScript - recursive call partial applied function

From Dev

How to call javascript function from <script> tag?

From Dev

How to call a Dart function from JavaScript

From Dev

How to call javascript function from asp:textbox?

From Dev

How to call javascript from Jquery function?

From Dev

How to call a JavaScript function from "ColdFusion button"

From Dev

How to call a Dart function from Javascript?

From Dev

How to call a JavaScript function from within Selenium?

From Dev

How to call javascript class function from closure

From Dev

How to call Java function dynamically from Javascript?

From Dev

How to call a C# function from JavaScript?

From Dev

How to call a Dart function from JavaScript

From Dev

how to call javascript function from Android

From Dev

how to call php function from javascript "duplicate"

From Dev

How to call parents function from child in Javascript

From Dev

How to call Ajax from Javascript function

From Dev

How to Stop a Recursive Function in JavaScript?

From Dev

How do I return from a recursive generator function in JavaScript?

From Dev

How to call a function in other function from html document - JavaScript.

From Dev

How to call a function in other function from html document - JavaScript.

From Dev

How to make a "call with reference" to recursive MatLab function

From Dev

How to properly call a recursive function inside a for loop?

Related Related

  1. 1

    From a cloud function: how to call JavaScript function that is recursive?

  2. 2

    How to call a recursive function in javascript

  3. 3

    Javascript: How to write and call a recursive function?

  4. 4

    How to call php function from JavaScript function?

  5. 5

    how to call javascript function from SmartGWT function

  6. 6

    How do I return javascript data from a function using a recursive call

  7. 7

    JavaScript recursive function breaks when () included in call

  8. 8

    JavaScript - recursive call partial applied function

  9. 9

    How to call javascript function from <script> tag?

  10. 10

    How to call a Dart function from JavaScript

  11. 11

    How to call javascript function from asp:textbox?

  12. 12

    How to call javascript from Jquery function?

  13. 13

    How to call a JavaScript function from "ColdFusion button"

  14. 14

    How to call a Dart function from Javascript?

  15. 15

    How to call a JavaScript function from within Selenium?

  16. 16

    How to call javascript class function from closure

  17. 17

    How to call Java function dynamically from Javascript?

  18. 18

    How to call a C# function from JavaScript?

  19. 19

    How to call a Dart function from JavaScript

  20. 20

    how to call javascript function from Android

  21. 21

    how to call php function from javascript "duplicate"

  22. 22

    How to call parents function from child in Javascript

  23. 23

    How to call Ajax from Javascript function

  24. 24

    How to Stop a Recursive Function in JavaScript?

  25. 25

    How do I return from a recursive generator function in JavaScript?

  26. 26

    How to call a function in other function from html document - JavaScript.

  27. 27

    How to call a function in other function from html document - JavaScript.

  28. 28

    How to make a "call with reference" to recursive MatLab function

  29. 29

    How to properly call a recursive function inside a for loop?

HotTag

Archive