Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Chats.js: remove async function inside Promise executor #113

Closed
1 task done
withSang opened this issue Aug 17, 2022 · 0 comments · Fixed by #117
Closed
1 task done

[Bug] Chats.js: remove async function inside Promise executor #113

withSang opened this issue Aug 17, 2022 · 0 comments · Fixed by #117
Assignees
Labels
😱 bug Something isn't working chatting

Comments

@withSang
Copy link
Member

withSang commented Aug 17, 2022

Describe the bug

Promise 객체를 만들 때 인자로 넣는 함수를 Promise executor라고 하는데요, Promise executor로 비동기 함수를 사용하는 것은 일반적으로 좋지 않은 코드 패턴이라고 여겨집니다.

그 이유는 크게 두 가지가 있는데, 먼저 Promise executor에서 핸들링되지 않은 오류가 발생하면 새로 만들어진 Promise가 reject를 반환하지 않습니다. 그리고 (거의 모든 경우) async 함수를 Promise constructor 밖으로 빼도 의도대로 잘 동작합니다.

참조 - https://eslint.org/docs/latest/rules/no-async-promise-executor

그래서 해당 코드를 수정하려고 합니다. chatsForRoom 함수를 async 함수로 만들면 간단하게 문제를 해결할 수 있다고 생각합니다.

  • 버그 해결하기

/**
* @todo no-async-promise-executor 오류 해결하기
*/
const chatsForRoom = (chats) => {
return new Promise(async (resolve, reject) => {
try {
const authorNames = {};
const authorProfileUrls = {};
const chatSend = [];
for (const chat of chats) {
if (!authorNames[chat.authorId]) {
const author = await userModel.findById(chat.authorId);
if (!author) {
return reject();
}
authorNames[author._id] = author.nickname;
authorProfileUrls[author._id] = author.profileImageUrl;
}
chat.inOutNames = [];
if (chat.type == "in" || chat.type == "out") {
const userIds = chat.content.split("|");
for (const userId of userIds) {
const user = await userModel.findOne({ id: userId });
if (!user) {
throw new IllegalArgumentsException();
}
chat.inOutNames.push(user.nickname);
}
}
chatSend.push({
type: chat.type,
authorId: chat.authorId,
authorName: authorNames[chat.authorId],
authorProfileUrl: authorProfileUrls[chat.authorId],
content: chat.content,
time: chat.time,
isValid: chat.isValid,
inOutNames: chat.inOutNames,
});
}
resolve(chatSend);
} catch (e) {
return reject();
}
});
};

이슈로 만들어도 될것 같아요 👍
Originally posted by @14KGun in #108 (comment)

To Reproduce

Steps to reproduce the behavior:

  1. 없음.

Screenshots

  • 없음.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
😱 bug Something isn't working chatting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant