As you can see the function returns asynchronously and does a console log. Unfortunately the issue is that I perform this action one time and it works, but when I try to run it again it the code runs (the console.log(‘run’) is executed … ) but the context.async does not.
I’m not sure why that’s the case. What am I missing here? Any help would be appreciated
I have this exact same problem while using context.async in plugin element actions.
I managed to run context.async several times on client-side actions only, with the exact same code.
I believe this issue deserves a bug report.
There are some mismatched design patterns in your use of promises and asynchronous functions:
Unless you need to extract a value from a promise or asynchronous function, or want to catch the promise rejection of a server side function in the server logs there is no need to wrap your asynchronous code in acontext.async.
You are passing the (undefined) result of calling console.log to the error handling part of the callback. This is problematic and will result in unpredictable behaviour.
Ditto for your promise result, you are passing the (undefined) result of console.log to a eventually a return statement. Again more unpredictable behaviour.
Pick either return Promise or async function for your design pattern, not both. I personally prefer the older promise chaining as it forces me, as a developer, to think explicitly about asynchronous dependencies and orders of operation.
I would refactor your code to stick to one design pattern. For example using promise chaining:
Note: there are browser and Node.js dependent idiosyncrasies when parsing anonymous functions. Some are more tolerant of partial syntax than others. I always use fully “curly brace” anonymous syntax to guard against parser errors.
Server-side pattern using context.async to mimic await behaviour:
Note: You can chain as many thens and Promise.alls or Promise.anys together outside of context.async before finally extracting a value with context.async
and it will run the async function every thime you trigger the action.
Of course it is an undocumented hack,
it can probably break other internal functionality, it will probably not work once bubble changes the plugin async api.
If you’re talking about element plugins, you can just use async/await syntax if you’re not loading any data. (If loading data, you’ll have to split your action into two parts, the first being the function that is called by the action code wherein you load your lists or things or whatnot and then calls an async function where you can await what needs to be awaited.)
Bubble Support recommended to try out context.v3.async but that does not apply here as this is about Plugin Element Actions.
The next and final message from Bubble support stated that they do not offer support for 'custom code (‘we’re unable to assist you further with custom code’).
nobody can confirm that that hack will not break in the future, that’s why you shouldn’t use it
when I wrote that post it was working, but it was a funny excercise and I never used it in production.