[Vuejs]-How to set new properties, or push to array with async and await, within callback function?

7👍

The issue is here:

data.forEach(async item => {

async functions return promises immediately when invoked, so the forEach completes and moves on to the console.log line.

The async work delegated to the async functions has not yet completed at this point and so the data is not yet modified.

Since it seems you are already in an async function (since you’ve used await higher in the code), you can wait for all the promises to resolve with await and Promise.all

Promise.all expects an array of promises, so instead of forEach we can use map to create an array of promises

await Promise.all( data.map(async item => { ...

Like forEach, map will iterate over all of the items and run the given function. Unlike forEach, map will return an array with the results of these functions, and given they are async functions, each one will return a promise.

Now we use Promise.all to create a single promise that will resolve when every one of the async functions have finished. And we use await to tell the function to pause until that new promise resolves.

Pausing the function with await means the console.log won’t run until every one of those async functions have finished, meaning it will have the correct data when it does run.

1👍

Here is a working example anyone who needs it:

  public async updateBatch(req:any,res:any){

        body = [1,3,4,5,6]

        var rfinal:number[] = [];

        await Promise.all(body.map(async items=>{

            let newStatus = 'MJ';

                inputParameters = [
                { name: 'PID', dataType: sql.Int, value: items },
                { name: 'Status', dataType: sql.VarChar, value: newStatus }
            ];

            let CustomQuery = `UPDATE MYTable 
                SET Status= @Status
                WHERE PID= @PID`;

            const result = await provider.executeQuery(CustomQuery, inputParameters).catch(err => {
                    LogErrors.logErrors(err);
            });
            if(result.rowsAffected[0]>0){
                    rfinal.push(result.rowsAffected[0]);
            }else{
                throw new console.error('Un expected error, updating newsfeed');
            }

        })
        );

  }
👤MJ X

Leave a comment