$w("#html).onMessage stack messages/runs multiple times in loop

Hey,
I have a loop that runs through all items and sends the address of the item to a html, in the html I use google geocoding to get the lat/lng and send it data back to the pagecode.

my code:

for (let item of res.items) {
    let address = item.address.formatted.replaceAll(" ", "+");
    $w("#html1").postMessage({ address: address });

    await new Promise((resolve) => {

        $w("#html1").onMessage(async (event) => {

            console.log("received message");

            item.lat = event.data.lat;
            item.lng = event.data.lng;

            await wixData.update(database, item);

            resolve();
        });
    });
}

But the console.log(“received message”) stacks with the loop, that means: in the first loop I get one console.log(), in the second loop I get two console.log(), …

I know this problem from javascript, if you use .addEventListener(“message”) multiple times, but it shouldn’t appear in a .onMessage.
Is there any solution in Velo to remove the event, or do you have an idea, how I can change the code?

Because if I’d use this code, I always get undefined back:

async function runLoop() {
    //get the items
    for (let item of res.items) {
        let address = item.address.formatted.replaceAll(" ", "+");
        $w("#html1").postMessage({ address: address });

        await new Promise((resolve) => {

            let msg = HTMLonMessage();

            console.log(msg);

            //update database

            resolve();
        });
    }
}

let HTMLonMessage = $w("#html1").onMessage((event) => {

    let lat = event.data.lat;
    let lng = event.data.lng;

    return [lat, lng];
});

hi, @nickoertel
on Editor X Forum usually there is design discussions, learning materials and problems sharing, as for code there are two platforms from our side, which could assist you:
Velo Forum
Editor X Facebook Group
I hope you will be able to find asstiance with your code there :hugs:

hi @andrewt ,
thanks, I reposted the question in Velo Forum.

Feel free to share the post here so we can find the answer here as well.

You’ll find the post here: