It depends on how you want your async code to be executed: sequentially or in parallel. Anyway, you'd need to add async
keyword to use await
.
// sequential
export default async (req, callback) => {
// ...
for(const [rule, index] of compliance.entries()) {
const response = await waRuleOverview(req, run.id, rule.id)
// handle the response
}
}
// parallel
export default async (req, callback) => {
// ...
const responses = await Promise.all(compliance
.map((rule, index) => waRuleOverview(req, run.id, rule.id))
)
// handle responses
responses.forEach(response => {
// ...
// handle response here
})
}
And finally, if you don't really want your handler to return a Promise but just want it to perform some async actions for side effects.
export default (req, callback) => {
// ...
compliance.forEach(/* add */ async (rule, index) => {
// to use await inside
let response = await waRuleOverview(req, run.id, rule.id);
// handle the response
});
}
But this approach is actually an anti-pattern since it breaks promise chains: bad for composability, error handling and such.