Output non correct

Hi, I try to explain .....
this is the code:

img1

const output = [null, null];

const x = msg.payload

if (x == 1)
{
    output[0] = { payload: "uno" }
    output[1] = { payload: "due" }
    setTimeout(() => { node.send(output[0]) }, 6000)
    setTimeout(() => { node.send(output[1]) }, 0)
}

I have the correct delay but for the same node (0) instead of "uno" for output [0]
and "due" for output [1]

img2

Where am I wrong?

That output looks correct to me. You are sending output[1] after a zero ms timeout so it appears first and output[0] after a 6s delay. What did you expect?

Of course, a timeout with a zero delay is a bit pointless I think.

The second image ....
both node: 0
Instead

output[1] = { payload: "due" }

should be node: 1

Urm, afraid not. Because you've only passed 1 element of output to each node.send. So both messages will appear from the top port of the function node and go to the debug marked 0 on your flow.

I think what you meant to do was something like this:

const output = [null, null];

const x = msg.payload

if (x == 1)
{
    output[0] = { payload: "uno" }
    output[1] = { payload: "due" }
    setTimeout(() => { node.send( [output[0], null] ) }, 6000)
    setTimeout(() => { node.send( [null, output[1]] ) }, 0)
}

If, instead, you did

const output = [null, null];

const x = msg.payload

if (x == 1)
{
    output[0] = { payload: "uno" }
    output[1] = { payload: "due" }
    setTimeout(() => { node.send(output) }, 6000)
    setTimeout(() => { node.send(output) }, 0)
}

You would get 4 outputs.

Try

const output = [[null,null], [null,null]];

const x = msg.payload

if (x == 1)
{
    output[0][0] = { payload: "uno" }
    output[1][1] = { payload: "due" }
    setTimeout(() => { node.send(output[0]) }, 6000)
    setTimeout(() => { node.send(output[1]) }, 0)
}

OR

const output = [[null],[null]]
const x = msg.payload

if (x == 1)
{
    output[0].unshift({ payload: "uno" })
    output[1].push({ payload: "due" })
    setTimeout(() => { node.send(output[0]) }, 6000)
    setTimeout(() => { node.send(output[1]) }, 0)
}
1 Like

Yes .....

const output = [null, null];

const x = msg.payload

if (x == 1)
{
    output[0] = { payload: "uno" }
    output[1] = { payload: "due" }
    setTimeout(() => { node.send( [output[0], null] ) }, 6000)
    setTimeout(() => { node.send( [null, output[1]] ) }, 0)
}

This is correct for me.
Thank you very much .....

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.