#Parallel property assigment

2 messages · Page 1 of 1 (latest)

woven spade
#

Hello NestJS community,
This might be more of a JavaScript-related question than NestJS-specific, but I’ve hit a roadblock with parallel processing of async methods. Specifically, in the following code, I'm waiting for each chat’s promise to resolve sequentially, which ends up being slow:

for (const member of response.chatMembers) {
  const chat: ChatsDto = member.chat;
  if (!chat.isGroup) {
    const secondUser = chat.chatMembers[0]?.user;
    if (!secondUser) throw new NotFoundException('Other user not found');
    chat.isOnline = await this.utilGateway.isUserOnline(secondUser.id);
  }
}

I’ve tried using Promise.all, but I can’t assign chat.isOnline without awaiting the isUserOnline function. As a result, Promise.all doesn’t seem to help in this case:

const chats = await Promise.all(
  t2.map(async (member) => {
    const chat: ChatsDto = member.chat;
    if (!chat.isGroup) {
      const secondUser = chat.chatMembers[0]?.user;
      if (!secondUser) throw new NotFoundException('Other user not found');
      chat.isOnline = await this.utilGateway.isUserOnline(secondUser.id);
    }
  })
);

I couldn’t find any solutions online, so I’d appreciate any advice from more experienced JS developers.

clear cave
#

you do not return anything in the map function btw