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

@ -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.
@ -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,"+
@ -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();
}
@ -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/);
@ -340,7 +340,7 @@ module.exports = function(RED) {
else {
done(JSON.stringify(err));
}
}
});
}
const RESOLVING = 0;