I have the following function that works as expected:
createObjectFrom(record) {
let obj = {};
this.opts.transformers.forEach((transformer, index) => {
const headerIndex = findIndex(this.headers, (header) => {
return header === transformer.column;
});
const value = transformer.formatter(record[headerIndex]);
obj[transformer.field] = value;
});
return obj;
}
I want to refactor it to use async await and call an async function in the body of the forEach like this:
createObjectFrom(record) {
let obj = {};
this.opts.transformers.forEach(async (transformer, index) => {
const headerIndex = findIndex(this.headers, (header) => {
return header === transformer.column;
});
const result = await this.knex('managers').select('name')
console.log(result);
const value = transformer.formatter(record[headerIndex]);
obj[transformer.field] = value;
});
return obj;
}
This will obviously break the function as the forEach is now executing asynchronously and the function will just execute and leave.
Is there a way I can use async await for the forEach to execute in a synchronous manner. Could I refactor to generators?