0

Here is my code.

let getMessage= await super.getResources(ctx, Flash, {where: {created_by: ctx.meta.userId}});
if(getMessage) {
    getMessage.forEach(employee);
    async function employee(item:any){
        console.log('Inside 1');
        if(item.effective_employees.length){
            console.log('Inside 2');
            for(let i=0;i<=(item.effective_employees.length -1);i++){
                let sql:string=`sqlQuery`;
                let data:any = await UserRepository.execQuery(ctx,Flash,sql);
                //let data = 1;
                console.log('Inside 3');
                if(data){
                    console.log('Inside 4');
                    item.effective_employees[i] = 'Data here from a function';
                    console.log(item.effective_employees);
                }
            }
            //return item;
        }
    }
    console.log('Inside 5');
    return Promise.resolve(getMessage);

If i am not using these lines or commenting out thease the return has Data here from a function in it.

            //let sql:string=`sqlQuery`;
            //let data:any = await UserRepository.execQuery(ctx,Flash,sql);

But if i use this without commenting the return type does not have Data here from a function In it.

I think my function is not waiting for the await and just passing the value which is there. How can I fix this?

xxoyx
  • 81
  • 8
  • Does this answer your question? [JavaScript async await doesn't work inside forEach loop](https://stackoverflow.com/questions/45914162/javascript-async-await-doesnt-work-inside-foreach-loop) – Ricky Mo Jan 27 '22 at 05:20
  • If there is an await inside forEach it will work asynchronously so remove forEach with for loop – hassanqshi Jan 27 '22 at 05:21
  • can some one give me an example here replacing for each? – xxoyx Jan 27 '22 at 05:33

0 Answers0