Run transform results async

Is it possible to run Transform Results asynchronously?

Case: I need to decrypt a field value with crypto.subtle.decrypt (which return a Promise)

Hi @davidsonsns,

Welcome to the community!

Yes, it is possible to run Transform Results asynchronously in Retool. You can use the async/await syntax to wait for the asynchronous operation to complete before returning the transformed data.

Here is an example of how to use async/await to decrypt a field value with crypto.subtle.decrypt:

async function transformResults(data) {
  const decryptedData = [];

  for (const row of data) {
    const encryptedFieldValue = row.encryptedField;
    const decryptedValue = await crypto.subtle.decrypt(alg, key, encryptedFieldValue);
    row.decryptedField = decryptedValue;
    decryptedData.push(row);
  }

  return decryptedData;
}

In this example, the transformResults function takes an array of data as input and returns an array of transformed data. The for loop iterates over the array of data, and for each row, it decrypts the encryptedField value using crypto.subtle.decrypt. The await keyword is used to wait for the asynchronous operation to complete before assigning the decrypted value to the decryptedField property of the row. Finally, the decrypted row is pushed onto the decryptedData array.

To use this code in Retool, you can create a JavaScript Transformer and paste the code into the transformer editor. Then, you can connect the transformer to a query resource. The transformer will be executed asynchronously, and the transformed data will be available to the next step in your flow.

Here are some additional tips for using async/await in Retool:

  • Make sure that the function that you are calling with await returns a Promise.
  • If you are calling multiple asynchronous operations, you can use Promise.all to wait for all of them to complete before continuing.
  • You can also use async/await to handle errors from asynchronous operations.

I hope this helps!

:grinning:

Patrick

Hi @davidsonsns

Javascript queries can be asynchronous; more info here in our docs! If you're using Javascript transformers, you may want to switch to Javascript queries which have more controls for when they trigger. Let us know if you were able to solve your case :blush:

Hi y'all, thanks for the answers (and sorry for the delay)!

So, not sure why im facing this issue:

> Error:Could not evaluate transformer: Evaluation result should be awaited
query1

in query1 transformer(query1)
from query1 response(query1)
> in query1.trigger()(query1)

Here's my transform code:

async function formatter(row) {
  return {
    legalName: await crypto.subtle.decrypt(alg, key, row.legalName),
    ...row
  }
}

return Promise.all(formatDataAsArray(data).map(formatter))

btw, im running this as a query transform, not as transformer.

Hi @davidsonsns I'm not sure that this will work in a query transformer :thinking: Does it work if you make it a JS query that runs on success of query1?

You'd need to change data in line return Promise.all(formatDataAsArray(data).map(formatter)) to query1.data

If you can share the URL for the crypto library, I can do some testing on my side