0

The code below is causing the following console error: Can not use keyword 'await' outside an async function.

async getData() {
    // an await occurs here for something else
                
    const menu = {
        'pounds': 'DemandModification',
        'combination': 'DemandCombinationOverride'
    };

    const overridesWithStaleBuckets = {};

    Object.entries(menu).forEach(o => {
        const overrideType = o[0];
        const api = o[1];

        const overrides = await this.$http.execute('read', api);
        overridesWithStaleBuckets[overrideType] = overridesWithStaleBuckets[overrideType] || {};
        overridesWithStaleBuckets[overrideType] = overrides;
    
    });

    // non problematic logic omitted

    return overridesWithStaleBuckets;
}

The console specifically calls out the await in this line as being the issue:

const overrides = await this.$http.execute('read', api);

Why is this error occurring when the function is async?

knot22
  • 2,648
  • 5
  • 31
  • 51

1 Answers1

0

You cannot use await in a forEach loop, you will need a normal for loop for this.

let entries = Object.entries(menu);
for (let entry of entries) {
  //Do things
}
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69