I'm currently working on a code that involves iterating over an array and performing asynchronous tasks for each element in the array. The objective is to execute async operations, retrieve a value from it and assign that value to an object's property before moving on to the next element. Ultimately, I aim to return the final object with all the properties set.
However, I've encountered an issue where my async code is not functioning correctly. The uninitialized variable is being returned prematurely, even before any of the async operations are completed. Additionally, I am unsure if the "await" placed before the forEach loop is having any effect.
generate(): Promise<Properties>{
return new Promise<Properties>(async resolve => {
let columns = this._propertyDefinitionRepo.columnNames;
let properties: Properties;
await columns.map(async columnName => {
return new Promise<void>(async resolve => {
let distinctValues = await this.doAsyncWork(columnName);
properties[this.columnNameToPropertyName(columnName)] =
this.buildPropertyDefinition(columnName, distinctValues);
resolve();
})
});
resolve(properties);
})
}