0

I have a big problem with recursion, callback and nodejs.

I have this object for exemple :

var a= [
    {index : 1},
    {index : 2},
    {index : [
        {index : 11},
        {index : 12},
        {index : [
            {index : 111},
            {index : 112},
            {index : "end-of-tree"},
        ]},
    ]},
    {index : 4},
]

And I would like to display sequentially this:

1
2
11
12
111
112
end-of-tree
4

with console.log(value)

In pure javascript it's easy but as node run asynchronously, it's a bit more complicated.

Who could help me ?

2
  • I don't get what is asych here. Looping through an array doesn't have to be async at all. Just use JS and it will work. Commented May 27, 2015 at 23:55
  • It's not just looping in and array. It's Recursif. To undersand the difference between pure javascript and nodejs concerning recursion, try this code in your web browser : function fib(num) { if (num==0 || num ==1) return num return fib(num-2) + fib (num-1) } And try the same thing in nodejs you will see it's amizing. Commented May 28, 2015 at 0:29

3 Answers 3

1

You could try something simpler like this!

function ff(item){
  var temp = item.index;
  if(temp instanceof Array) temp.forEach(ff);
  else console.log(temp);   
}
a.forEach(ff);

In fact your problem doesn't have anything asynchronous it is a synchronous itteration in an array!

Sign up to request clarification or add additional context in comments.

4 Comments

In fact the problem still remain. When I apply oracle queries inside the loop, the order of the tree is no more respected. Instead of 1 2, 11, 12, 111, 112... I get 1,2,4,6.. first then the result of the chidren loops.
can you please explain more? or even create a new question with a sample code of what you are doing. Maybe this oracle query is asynchronous, and if it is, the code i posted will not work!
Hello alex-rokabilis. This module solved my problem : justinklemm.com/node-js-async-tutorial
I mixed it with the code that you gave me. It works great. async.each() is especially design to do forEach loops asynchrounously. Thanks very much for you support.
0

I manage to get it to work.

JSON exemple :

var a= [
    {index : 1},
    {index : 2},
    {index : [
        {index : 11},
        {index : 12},
        {index : [
            {index : 111},
            {index : 112},
            {index : "end-of-tree 1xx"},
        ]},
    ]},
    {index : 4},
    {index : [
        {index : 51},
        {index : 52},
        {index : [
            {index : 531},
            {index : 532},
            {index : [
                {index : 5341},
                {index : 5342},
                {index : [
                    {index : 53441},
                    {index : 53442},
                    {index : "end-of-tree 5xx"},
                ]}, 
            ]},
        ]},
    ]},
    {index : 6},
    {index : 7},
    {index : 8},
    {index : 9},    
    {index : [
        {index : 'A0'},
        {index : 'A1'},
        {index : [
            {index : 'A21'},
            {index : 'A22'},
            {index : [
                {index : 'A311'},
                {index : 'A312'},
                {index : [
                    {index : 'A3131'},
                    {index : 'A3132'},
                    {index : "end-of-tree Axx"},
                ]}, 
            ]},
        ]},
    ]}, 
]

Javascript code :

function tree(json) {
    item=json[0];
    if (item) {
        if (Array.isArray(item.index)) {
            tree(item.index);
        } else {
            console.log(item.index)
            json.shift();
            if (json) tree(json);
        }       
    } else {
        a.shift();
        if (a.length>0) tree(a);
    }
} 

tree(a)

Console Log result :

1
2
11
12
111
112
end-of-tree 1xx
4
51
52
531
532
5341
5342
53441
53442
end-of-tree 5xx
6
7
8
9
A0
A1
A21
A22
A311
A312
A3131
A3132
end-of-tree Axx

Comments

0

@alex-rocabillis.... https://github.com/oracle/node-oracledb/blob/master/doc/api.md#-523-execute

oracle execute fonction is asynchronous. I gonna create an other post to explain the problem.

5.2.3 execute()

Prototype

void execute(String sql, [Object bindParams, [Object options,]] function(Error error, [Object result]){}); Return Value

None

Description

This call executes a SQL or PL/SQL statement. See SQL Execution for examples.

This is an asynchronous call.

The statement to be executed may contain IN binds, OUT or IN OUT bind values or variables, which are bound using either an object or an array.

A callback function returns a result object, containing any fetched rows, the values of any OUT and IN OUT bind variables, and the number of rows affected by the execution of DML statements.

Parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.