Repository: pedroslopez/whatsapp-web.js
File: src/structures/GroupChat.js (JavaScript)

Code before:
                }

                sleep &&
                    participantWids.length > 1 &&
                    participantWids.indexOf(pWid) !== participantWids.length - 1 &&
                    (await new Promise((resolve) => setTimeout(resolve, _getSleepTime(sleep))));
            }

            return participantData;
        }, this.id._serialized, participantIds, options);
    }

    /**
     * Removes a list of participants by ID to the group
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>}
     */
    async removeParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return (chat.groupMetadata.participants.get(lid._serialized) || chat.groupMetadata.participants.get(phone._serialized));
            });
            await window.Store.GroupParticipants.removeParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**
     * Promotes participants by IDs to admins
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
     */
    async promoteParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return (chat.groupMetadata.participants.get(lid._serialized) || chat.groupMetadata.participants.get(phone._serialized));
            });
            await window.Store.GroupParticipants.promoteParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**

Review comment (suggestion):
The resulted array have to be filtered out of all the undefined objects that can be there.
Also the optional chaining operator have to be added in a case of `lid` or `phone` are undefined.

```suggestion
                return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
            }).filter(p => Boolean(p));
```

Code after:
                }

                sleep &&
                    participantWids.length > 1 &&
                    participantWids.indexOf(pWid) !== participantWids.length - 1 &&
                    (await new Promise((resolve) => setTimeout(resolve, _getSleepTime(sleep))));
            }

            return participantData;
        }, this.id._serialized, participantIds, options);
    }

    /**
     * Removes a list of participants by ID to the group
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>}
     */
    async removeParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
            }).filter(p => Boolean(p));
            await window.Store.GroupParticipants.removeParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**
     * Promotes participants by IDs to admins
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
     */
    async promoteParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
            }).filter(p => Boolean(p));
            await window.Store.GroupParticipants.promoteParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**