Recursion, callback and nodejs

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

I have this object for example:

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 this consistently:

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

      

using console.log (value)

In pure javascript this is easy, but as node is executed asynchronously it is a little more complicated.

Who could help me?

0


source to share


3 answers


You can 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);

      



Actually your problem has nothing asynchronous, it's a synchronous iteration on an array!

+1


source


I manage to get it to work.

JSON example:

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

      

0


source


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

oracle execute fonction is asynchronous. I'll post another 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 is executed by an SQL or PL / SQL statement. See SQL Execution for examples.

This is an asynchronous call.

The statement to be executed can contain IN binds, OUT, or IN OUT bind values ​​or variables that are associated with the use of either an object or an array.

The callback function returns a result object containing any selected rows, the values ​​of any OUT and IN OUT binding variables, and the number of rows that affect the execution of the DML statements.

Parameters

0


source







All Articles