Async registerType callback?

I might be a bit thick today but I don't seem to be able to find any magic to allow the RED.nodes.registerType callback function to be an async function?

I have a function call in it that I would ideally like to await and that has to be async.

The only other approach is a major re-write to push all of the follow-up processing into a promise .then() which is fairly horrible and hard to follow.

Anyone have any other ideas?

Sheesh! And isn't that typical. Immediately after I posted this, I worked it out!

You simply add an IIFE wrapper.

// ...
(async () => {
    try {
        await someAsyncPromisesFn()
    } catch (e) {
        log.error('It failed you numpty!`, e)
    }
})()
// ...

I've been trying to work that out for ages! :smiley:

Of course, the outer function keeps going and doesn't wait - but in this case, that is fine as everything will be complete before the instance setup completes.

You numpty!!

<is the code I’m reading> :grin:

Yup, that's how I feel right now! :+1:

1 Like

I'm trying to dig myself out of a different hole I created when I set up uibuilder's templating system.

I allowed for external templates but I didn't allow for a "standard" template to be external, only internal. Over time that has come back to haunt me as some of the templates are examples of using front-end frameworks.

As we know, FE frameworks drift over time and now, trying to maintain them within the uibuilder module has become increasingly burdensome. So I want to dump them out but I still want to have a list people can refer to.

That means firstly - simplifying the template setup. Then adapting the setup function. It has to be async because the external part relies on degit which is purely async.

Once I can do this, I can easily update the more complex templates and have even more readily available showing more complex examples and including more integrated build tools and the like.

Can you share your solution? I did not see any registerType with a callback yet

Client registration:

Server registration:

All of my nodes follow the same format. The 2nd parameter of RED.nodes.registerType references a function and it is that function that I wanted to make async. But if I try, the node won't load. It is that function that is run when an instance of your node is deployed in a flow.

I've worked around the issue by using an IIFE wrapped around an async section because I didn't actually need the whole fn to be async in the end.

What is the goal with this pattern? You want to have the construction of all instances of the same node type running in series?

Sorry, not sure what you mean?

Every instance of a node MUST have its own settings and processing separate from every other node. Behind that independent instance process you have a common set of data and processes.

If you don't have an equivalent of what I call nodeInstance but many nodes have as an embedded function (which really isn't good practice), you can never have more than one set of settings shared between all instances of a node type.

In my nodes, I flatten the functional structure to make it easier to follow and to reduce the need for implied data inheritence.

My testbed node module is probably easiest to follow:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.