How to use async callback in bubble.io plugin builder (context.async)

Last modified date

Comments: 0

Contents

Problem

When creating a custom plugin on bubble.io, you can run Javascript code (using Node) on the server side of the application.

Sometimes this server side code will use async functions to make requests to other services (e.g. external api’s).

Typically you could use a Promise or async functions but this does not work in bubble.io plugins as stated in their documentation:

Note: When writing server-side actions, avoid using async/await syntax to wait for promises. The framework we use to to handle asynchronous code (notably used in context.async or context.request) relies on a special library called “Fibers”, which works very differently from usual promises and will cause unexpected behavior if async/await is used. Consider using the traditional Promise.then/catch syntax instead, which is supported by Fibers.

https://manual.bubble.io/account-and-marketplace/building-plugins/building-actions

If you try to use a Promise and standard async/await protocols, the bubble server side executor will not recognise to await on these functions and instead will just continue to execute the rest of the code and return immediately rather than waiting.

Solution

Instead you have to use the context object passed into the main function to handle your async calls.

This is a working example of how this works.

Using the context.async you can pass a function into it which bubble will run in an async manner.

In this example, the timeout will cause the bubble servers to wait 5 seconds before continuing with the script. As you can see in the console, the request took over 5 seconds and after 5 seconds it printed the message ‘return from async callback’ in the console.

JakTech

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.