0

I am using node-red flow and inside I have a node function that does something like this:

s3.getObject(params, function(err,data) {

    if (!err && data && data.Body) {
        msg.s3aka = data.Body.toString()
    }
});

while (true) {
    if (msg.s3aka)
        break;
    let start = new Date().getTime();
    while (true) { // delay 100ms
        if ((new Date().getTime() - start) > 100){
            break;
        }
    }
}

return msg;

So I wait for the result (msg.s3aka) to be set to return the msg. This looks ugly and I can't use async/await inside node function (not support ES 7). If I use Promise the last line (return msg) will be executed before msg.s3aka is set. So is there a better way to do this using vanilla javascript?

Edit: This is not solved by How do I return the response from an asynchronous call? since it is specific to node-red. I tried various ways including using date, async await, promise. I solved this by using node.send(msg) in the callback.

Viet
  • 12,133
  • 2
  • 15
  • 21
  • Nothing about `s3.getObject` and its asynchronous callback is specific to node-red? – Bergi Dec 21 '20 at 09:20
  • And the Node-RED function editor may flag errors but still run the code if Node-RED is running on a new enough version of NodeJS, it's just that code editor doesn't understand the new syntax – hardillb Dec 21 '20 at 14:25

0 Answers0