Receive undefined when using loop / while or foreach

I am trying out to figure out why I am receiving a undefined from the backend .
I often having that issue when having a for loop or foreach function.

BackEnd:

export async function calculTarif(selChalet, selFrom, selTo, selFromNum, selToNum, isMember, isInviter, isVisiteur, selFamMbr, selFamInv, selFamVis, member) {

             return wixData.query("myParams")
                .eq("title", "Reservation")
                .eq("userId", member._id)
                .find()
                .then(results => {
                    let rowParam = results.items[0];
                    console.log("±±±read myparam");
                    return rowParam;
                })
                .then(async rowParam => {
                    //if (rowParam.period !== null && rowParam.period !== undefined) {

                    let total = 0;
                    if (period.length > 0) {
                        await period.forEach(lineItem => {
                        ...
                        });
                    }
                    console.log("±±± to update rowParam ");
                    rowParam.total = await total;
                    rowParam.period = await period;

                    setTimeout(() => {
...
                    }, 1000);
                    return rowParam;
                })
                .then((rowParam) => {
                    return wixData.update("myParams", rowParam, options)
                        .then(results => {
                            console.log("±±±saved myparam");
                            ...
     }

I tried using async / await, I also tried to put some settimout() with no clue.:
How can we hold the execution of the next statement to wait for the ending of execution of the current statement?
The last .then() need the data from rowParam, but received undefined when effectively there are data.

Hi Youge,

First of all, when using async/await don’t use .then or otherwhise.
So.
when your function calculTarif should not have an async since you don’t wait for your query.

also this seems a bit fishy.

rowParam . total = await total ;
rowParam . period = await period ;

When using async/await it should only be when receiving a promise.
total & period are no functions so they already hold a value.
This means you don’t have to wait for it.

Also period . forEach

will not return a promise since its a loop.
You should not wait for it.

Small example

for ( let i = 0 ; i <= 100000 ; i ++) {
if ( i < 100 ){
console . log ( "Counting… : " + i )
} else if ( i === 100 ){
console . log ( “Hey i made it!” )
} else if ( i === 1000 ){
console . log ( “Did i made it second?” )
} else if ( i === 5000 ){
console . log ( “Is it my turn now?” )
} else if ( i === 100000 ){
console . log ( “wow finaly made it” )
}
}
console . log ( “Damn i’m last!” )

//output:
Counting… : 1
Counting… : 2
Count… till 99
Hey i made it!
Did i made it second
Is it my turn now?
Wow finaly made it!
Damn i’m last!

A for loop will finish first before it does something else.

Kind regards,
Kristof

problem solve, thanks for the advise @volkaertskristof