I am unable to understand how I can access elements from an array in an asynchronous fashion. I have looked at
Async functions in for loops javascript
and
Using async/await with a forEach loop
but to no avail.
My functions
export async function processUserAllMessages() {
let messages = await getAllMessagesByRoom();
var message;
for (let message_obj of messages) {
message = message_obj.data;
let message_ref = message_obj.ref['@ref'].id;
if (message.to == get(user).username) {
if (message.acknowledged) {
await deleteMessage(message_obj);
} else {
let promise = await processMessage(message_obj);
console.log(message_ref)
}
}
}
return true;
}
async function processMessage(message_obj) {
// code to process message
return promise
Typical output
340877637764252236
340877637764252236
340877637764252236
340877637912101449
340877637912101449
340877637912101449
340877637916295753
340877637916295753
340877637916295753
The messages are returned in the for loop multiple times. How can I fix this?