Run code - not resolving / ending

Hi,

I’m working on encoding a picture to as base64 string and I managed to create run code script that works. But when I try to run it for each picture in sequence, it never finished the first iteration - seems like it never comes to a resolve / reject. But I see that the base64 string i set on the object - so the code runs the actions with the variable.

Both me and ChatGPT is out of ideas - any skilled developers here that could take a look?
The code encodes the file content URL of the picture and returns the base64 string to a action variable and runs the actions “updatePicture” this sets the base64 in a string field in the run time object.

 return new Promise((resolve, reject) => {
        fetch(fileContentURL)
            .then(response => response.blob())  // Fetch the image as a Blob
            .then(blob => {
                const reader = new FileReader();

                // Once the reader finishes reading, resolve the promise
                reader.onloadend = () => {
                    const base64 = reader.result; // The base64 string
                    updatePicture({ base65String: base64 })  // Call the function with base64 string
                        .then(resolve)  // Resolve the promise with the update result
                        .catch(reject);  // Handle errors from the updatePicture function
                };
              
                reader.onerror = (error) => {
                    reject(error); // Reject the promise if FileReader encounters an error
                };

                reader.readAsDataURL(blob); // Start reading the blob as a data URL
            })
            .catch(reject);  // Reject the promise if fetch or blob fails
    });

Hi, are you using the run code component?

In that case, you do not need to return a Promise.
Instead, just calling resolve() or reject() will signal to Create that the code is finished. Here are some examples, showcasing how to do so with Promises and try-catch

asyncFunction({
    "param1": value1
})
    .then(() => resolve())
    .catch((error) => reject(error))
try {
    aFunction()
    resolve()
} catch (error) {
    reject(error)
}

You might be able to rewrite your function as:

fetch(fileContentURL)
            .then(response => response.blob())  // Fetch the image as a Blob
            .then(blob => {
                const reader = new FileReader();

                // Once the reader finishes reading, resolve the promise
                reader.onloadend = () => {
                    const base64 = reader.result; // The base64 string
                    updatePicture({ base65String: base64 })  // Call the function with base64 string
                        .then(resolve)  // Resolve the promise with the update result
                        .catch(reject);  // Handle errors from the updatePicture function
                };
              
                reader.onerror = (error) => {
                    reject(error); // Reject the promise if FileReader encounters an error
                };

                reader.readAsDataURL(blob); // Start reading the blob as a data URL
            })
            .catch(reject);  // Reject the promise if fetch or blob fails

Hope this helps.

// Erik

2 Likes

Seems to work perfectly - thanks ! saved me many hours of headache :slight_smile:

1 Like