1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

[function] Make the function node top-level async

This allows you to use 'await' in a function node without
having to wrap it in another promise/async function.
This commit is contained in:
Nick O'Leary 2020-05-22 20:49:18 +01:00
parent 22e7ddcb1d
commit 7969dd431f
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -105,7 +105,7 @@ module.exports = function(RED) {
} }
var functionText = "var results = null;"+ var functionText = "var results = null;"+
"results = (function(msg,__send__,__done__){ "+ "results = (async function(msg,__send__,__done__){ "+
"var __msgid__ = msg._msgid;"+ "var __msgid__ = msg._msgid;"+
"var node = {"+ "var node = {"+
"id:__node__.id,"+ "id:__node__.id,"+
@ -284,14 +284,14 @@ module.exports = function(RED) {
} }
function processMessage(msg, send, done) { function processMessage(msg, send, done) {
try {
var start = process.hrtime(); var start = process.hrtime();
context.msg = msg; context.msg = msg;
context.send = send; context.send = send;
context.done = done; context.done = done;
node.script.runInContext(context); node.script.runInContext(context);
sendResults(node,send,msg._msgid,context.results,false); context.results.then(function(results) {
sendResults(node,send,msg._msgid,results,false);
if (handleNodeDoneCall) { if (handleNodeDoneCall) {
done(); done();
} }
@ -302,7 +302,7 @@ module.exports = function(RED) {
if (process.env.NODE_RED_FUNCTION_TIME) { if (process.env.NODE_RED_FUNCTION_TIME) {
node.status({fill:"yellow",shape:"dot",text:""+converted}); node.status({fill:"yellow",shape:"dot",text:""+converted});
} }
} catch(err) { }).catch(err => {
if ((typeof err === "object") && err.hasOwnProperty("stack")) { if ((typeof err === "object") && err.hasOwnProperty("stack")) {
//remove unwanted part //remove unwanted part
var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/); var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/);
@ -340,7 +340,7 @@ module.exports = function(RED) {
else { else {
done(JSON.stringify(err)); done(JSON.stringify(err));
} }
} });
} }
const RESOLVING = 0; const RESOLVING = 0;