addToCart( ) It does not work properly [solved]

I have this piece of code:

function createNewProduct() {
 const newProduct = {
 'name': $w('#iptNomePacco').value,
 'description': JSON.stringify(secondRepeaterData, ["nome", "quantity"]),
 'price': totaleDaPagare,
 'productType': 'physical'
    };

    createCustomProduct(newProduct)
        .then((result) => {
 const src = 'https://static.wixstatic.com/media/d6e20a_b6708b98d1944a91a3344bf2b89f3e58~mv2.jpg';
            addMediaToProduct(result._id, [{ src }])
                .then((response) => {
                    $w('#shoppingCartIcon').addToCart(result._id)
                        .then(() => {
                            console.log("Product added");
                        })
                        .catch((error) => {
                            console.log(error);
                        });
                    $w('#goToCheckout').show();

                    idProdotto = result._id;

 let customID = Math.floor(Math.random() * 90000) + 10000000;
 let toInsert = {
 '_id': customID.toString(),
 'nomePacco': $w('#iptNomePacco').value,
 'cosaContiene': JSON.stringify(secondRepeaterData, ["nome", "quantity"]),
 'idProdottoStore': idProdotto,
 'prezzo': totaleDaPagare.toString()
                    }
                    $w('#txtAggiuntoAlCarrello').show();
                    wixData.insert('cesteCustom', toInsert)
                        .catch((err) => {
                            console.log(err)
                        });

                })
                .catch((error) => {
                    console.error('Error in createCustomProduct', error);
                })
        })
        .catch((error) => {
            console.error('Error in createCustomProduct', error);
        });
}

which creates a new product, adds it to the cart and then saves everything in a database.
It works correctly but addToCart () sometimes gives me an error and sometimes it doesn’t, I can’t really understand, the code doesn’t change but sometimes it doesn’t add to the cart and sometimes it does.

Can anyone help me figure out how to correct this?

The code is a little hard to follow. On the surface, it looks to me that perhaps some of the code isn’t being executed after the Promises.

I would suggest trying to simplify the process of addCart() so you can identify where the problem is.

Understand that the forum is not a support site, and we are unable to debug complicated code. Can’t promise anything, but post the URL to your site, and explain how the problem can be reproduced and I’ll take a look.

I found the error, you had to add a timeout for the add to cart because otherwise it was done before the store could create the product id

@bigimatt14 Aha! So now it really sounds like it could be an issue with Promises. You shouldn’t have to do a timeout if you wait for the store to first create the product id, and then add to the cart.

Check out your code sequence and make sure that you are adding to cart after the Store returns a Promise.

@yisrael-wix Yes, but the point is that it should already be executed after a .then () promise so it’s a little weird

createCustomProduct(newProduct)
        .then((result) => {