Multiple Messages with setTimeout

How can i push settimeout function value as output in function node ?
I want to send O1 as output-1 and O2/rest messages as output-2

Things I've tried

var O1=({payload: "hello1"});
var O2=({payload: "hello2"});
setTimeout(function(){
({payload: "hello3"});
}, 1000);
setTimeout(function(){
({payload: "hello4"});
}, 2000);

return [ O1, O2 ];

if i add node.send then it sends output but via O1

var O1=({payload: "hello1"});
var O2=({payload: "hello2"});
var O3=setTimeout(function(){
({payload: "hello3"});
}, 1000);
var O4=setTimeout(function(){
({payload: "hello4"});
}, 2000);

return [ O1, [ O2, O3, O4 ] ];

This throws error "[object Object]" as msg : Timeout

It there a particular reason you are not doing this low-code (i.e. using delay node)?

Anyhow, a msg MUST be an object (O3 and O4 are the result of calling setTimeout which are simply positive integers)

If you REALLY must (or are simply trying to learn) then to send O1 out of pin 1 and O2, O3 & O4 out of pin 2 then...

const O1 = { payload: "hello1" };
const O2 = { payload: "hello2" };
setTimeout(function () {
    node.send([null, { payload: "hello3" }]);
}, 1000);
setTimeout(function () {
    node.send([null, { payload: "hello4" }]);
}, 2000);

return [O1, O2];

EDIT: Updated for typos - thanks @UnborN

Yes , for learning purpose

Thanks for code but there is no output of hello3 and hello4 after timeout at pin 2

There was typo .. should be node.send([{ payload: "hello3" }, null]);
also for node.send([null, { payload: "hello4" }]);

1 Like

Yeah, I should have actually tested it :person_facepalming:

Thank you , solved

@UnborN thank you for noticing typo

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