From afb566b6b4a4b2387ab5e300f35abd40fdcb1edf Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 7 Jul 2018 22:09:55 +0100 Subject: [PATCH] Add async context support to Inject node --- nodes/core/core/20-inject.js | 40 +++++++++++++++++++++++------------- red/runtime/util.js | 2 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/nodes/core/core/20-inject.js b/nodes/core/core/20-inject.js index 9e6eb62a8..c0d9e0c2f 100644 --- a/nodes/core/core/20-inject.js +++ b/nodes/core/core/20-inject.js @@ -63,21 +63,33 @@ module.exports = function(RED) { } this.on("input",function(msg) { - try { - msg.topic = this.topic; - if ( (this.payloadType == null && this.payload === "") || this.payloadType === "date") { - msg.payload = Date.now(); - } else if (this.payloadType == null) { - msg.payload = this.payload; - } else if (this.payloadType === 'none') { - msg.payload = ""; - } else { - msg.payload = RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg); + msg.topic = this.topic; + if (this.payloadType !== 'flow' && this.payloadType !== 'global') { + try { + if ( (this.payloadType == null && this.payload === "") || this.payloadType === "date") { + msg.payload = Date.now(); + } else if (this.payloadType == null) { + msg.payload = this.payload; + } else if (this.payloadType === 'none') { + msg.payload = ""; + } else { + msg.payload = RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg); + } + this.send(msg); + msg = null; + } catch(err) { + this.error(err,msg); } - this.send(msg); - msg = null; - } catch(err) { - this.error(err,msg); + } else { + RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg, function(err,res) { + if (err) { + node.error(err,msg); + } else { + msg.payload = res; + node.send(msg); + } + + }); } }); } diff --git a/red/runtime/util.js b/red/runtime/util.js index 7d6464174..6279c416c 100644 --- a/red/runtime/util.js +++ b/red/runtime/util.js @@ -368,7 +368,7 @@ function evaluateNodeProperty(value, type, node, msg, callback) { if (callback) { callback(result); } else { - return value; + return result; } }