Skip to content

Commit

Permalink
[function] Make the function node top-level async
Browse files Browse the repository at this point in the history
This allows you to use 'await' in a function node without
having to wrap it in another promise/async function.
  • Loading branch information
knolleary committed May 22, 2020
1 parent 22e7ddc commit 7969dd4
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/node_modules/@node-red/nodes/core/function/10-function.js
Expand Up @@ -96,7 +96,7 @@ module.exports = function(RED) {
node.fin = n.finalize ? n.finalize : "";

var handleNodeDoneCall = true;

// Check to see if the Function appears to call `node.done()`. If so,
// we will assume it is well written and does actually call node.done().
// Otherwise, we will call node.done() after the function returns regardless.
Expand All @@ -105,7 +105,7 @@ module.exports = function(RED) {
}

var functionText = "var results = null;"+
"results = (function(msg,__send__,__done__){ "+
"results = (async function(msg,__send__,__done__){ "+
"var __msgid__ = msg._msgid;"+
"var node = {"+
"id:__node__.id,"+
Expand Down Expand Up @@ -284,14 +284,14 @@ module.exports = function(RED) {
}

function processMessage(msg, send, done) {
try {
var start = process.hrtime();
context.msg = msg;
context.send = send;
context.done = done;
var start = process.hrtime();
context.msg = msg;
context.send = send;
context.done = done;

node.script.runInContext(context);
sendResults(node,send,msg._msgid,context.results,false);
node.script.runInContext(context);
context.results.then(function(results) {
sendResults(node,send,msg._msgid,results,false);
if (handleNodeDoneCall) {
done();
}
Expand All @@ -302,7 +302,7 @@ module.exports = function(RED) {
if (process.env.NODE_RED_FUNCTION_TIME) {
node.status({fill:"yellow",shape:"dot",text:""+converted});
}
} catch(err) {
}).catch(err => {
if ((typeof err === "object") && err.hasOwnProperty("stack")) {
//remove unwanted part
var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/);
Expand Down Expand Up @@ -340,7 +340,7 @@ module.exports = function(RED) {
else {
done(JSON.stringify(err));
}
}
});
}

const RESOLVING = 0;
Expand Down

0 comments on commit 7969dd4

Please sign in to comment.