[JavaScript] Using synchronous AND asynchronous code in plugin actions, anyone had any luck with this?

Here’s an example from a current project… I will walk you through the steps:

  1. This workflow finishes by calling some code within a plugin (defined as an Action)

  2. Inside the plugin that looks like this, and I have highlighted where the async operation kicks-off…

  3. Most async operations in JS require you to specify a ‘callback’ function… i.e. to specify what code should run once the async operation returns a result. In this example I am making a GET call to Google Sheets. And below, I am showing you some code from my callback function, and in that code I trigger a Bubble Event - I have called it ‘data_initialised’ (and I have defined it within ‘Events’ higher up the plugin page)…

  4. Then, finally, I use that Event to kick-off another workflow that completes the operation that was begun in Step 1. That workflow is triggered by the Event that was called within the code in Step 3. See below…

    .

So… to summarise:
(1) Trigger some plugin code from your Workflow, maybe as an Action as I did.
(2) Within this plugin, kick-off your async operation.
(3) As part of your async operation (e.g. some type of API / XML request) you will define a Callback function, to be run when the async operation is completed… and within this function you should trigger a Bubble Event.
(4) Use this event to trigger another Workflow, and you now know that that the async operation is complete.

5 Likes